2007年10月14日 星期日 11:25
最近在看ruby,ruby实现singleton模式和类的属性都比较简单,比如
class Singleton
private_class_method :new
@@instance = nil
def Singleton.instance
@@instance = new if ! @@instance
#or
#@@instance = new unless @@instance
end
def log(msg)
puts "instance write: #{msg}"
end
end
类的属性也很简单
class CTest
@@index = 1
attr :name,true
def initialize(name)
@name = name
end
只要用attr关键字就可以很容易实现一个class的属性,
我网上搜索了一下,python对应的方法相对比较复杂,有什么简单的方法吗?谢谢
--
2007年10月14日 星期日 14:19
class C(object):
def __init__(self): self._x = None
def getx(self): return self._x
def setx(self, value): self._x = value
def delx(self): del self._x
x = property(getx, setx, delx, "I'm the 'x' property.")
在 07-10-14,pyman577<pyman577 at sina.com> 写道:
> 最近在看ruby,ruby实现singleton模式和类的属性都比较简单,比如
> class Singleton
> private_class_method :new
> @@instance = nil
> def Singleton.instance
> @@instance = new if ! @@instance
> #or
> #@@instance = new unless @@instance
> end
> def log(msg)
> puts "instance write: #{msg}"
> end
> end
>
> 类的属性也很简单
> class CTest
> @@index = 1
> attr :name,true
> def initialize(name)
> @name = name
> end
> 只要用attr关键字就可以很容易实现一个class的属性,
> 我网上搜索了一下,python对应的方法相对比较复杂,有什么简单的方法吗?谢谢
> --
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
--
个人网站:http://zsp007.com.cn/
专业:生物医学工程+计算机科学与技术
技能:C++(STL,BOOST) Python(Django) HTML+CSS AJAX
-- 张沈鹏
2007年10月14日 星期日 14:24
singleton参见 http://www.chenwy.com/python-iaq-cn.html http://wiki.woodpecker.org.cn/moin/SingletonPattern 在 07-10-14,张沈鹏(电子科大08年本科应届)<zsp007 at gmail.com> 写道: > class C(object): > def __init__(self): self._x = None > def getx(self): return self._x > def setx(self, value): self._x = value > def delx(self): del self._x > x = property(getx, setx, delx, "I'm the 'x' property.") > > > 在 07-10-14,pyman577<pyman577 at sina.com> 写道: > > 最近在看ruby,ruby实现singleton模式和类的属性都比较简单,比如 > > class Singleton > > private_class_method :new > > @@instance = nil > > def Singleton.instance > > @@instance = new if ! @@instance > > #or > > #@@instance = new unless @@instance > > end > > def log(msg) > > puts "instance write: #{msg}" > > end > > end > > > > 类的属性也很简单 > > class CTest > > @@index = 1 > > attr :name,true > > def initialize(name) > > @name = name > > end > > 只要用attr关键字就可以很容易实现一个class的属性, > > 我网上搜索了一下,python对应的方法相对比较复杂,有什么简单的方法吗?谢谢 > > -- > > > > > > _______________________________________________ > > python-chinese > > Post: send python-chinese at lists.python.cn > > Subscribe: send subscribe to python-chinese-request at lists.python.cn > > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > -- > 个人网站:http://zsp007.com.cn/ > 专业:生物医学工程+计算机科学与技术 > 技能:C++(STL,BOOST) Python(Django) HTML+CSS AJAX > -- 张沈鹏 > -- 个人网站:http://zsp007.com.cn/ 专业:生物医学工程+计算机科学与技术 技能:C++(STL,BOOST) Python(Django) HTML+CSS AJAX -- 张沈鹏
2007年10月20日 星期六 08:41
Python in a Nutshell的第五章讲Metaclass的时候给了一个Singleton的实例。抄一下就行了。 注意用法。新的类只要继承这个类,就自动是Singleton的了。 On 10/14/07, 张沈鹏(电子科大08年本科应届) <zsp007在gmail.com> wrote: > singleton参见 > http://www.chenwy.com/python-iaq-cn.html > http://wiki.woodpecker.org.cn/moin/SingletonPattern > > 在 07-10-14,张沈鹏(电子科大08年本科应届)<zsp007在gmail.com> 写道: > > class C(object): > > def __init__(self): self._x = None > > def getx(self): return self._x > > def setx(self, value): self._x = value > > def delx(self): del self._x > > x = property(getx, setx, delx, "I'm the 'x' property.") > > > > > > 在 07-10-14,pyman577<pyman577在sina.com> 写道: > > > 最近在看ruby,ruby实现singleton模式和类的属性都比较简单,比如 > > > class Singleton > > > private_class_method :new > > > @@instance = nil > > > def Singleton.instance > > > @@instance = new if ! @@instance > > > #or > > > #@@instance = new unless @@instance > > > end > > > def log(msg) > > > puts "instance write: #{msg}" > > > end > > > end > > > > > > 类的属性也很简单 > > > class CTest > > > @@index = 1 > > > attr :name,true > > > def initialize(name) > > > @name = name > > > end > > > 只要用attr关键字就可以很容易实现一个class的属性, > > > 我网上搜索了一下,python对应的方法相对比较复杂,有什么简单的方法吗?谢谢 > > > -- > > > > > > > > > _______________________________________________ > > > python-chinese > > > Post: send python-chinese在lists.python.cn > > > Subscribe: send subscribe to python-chinese-request在lists.python.cn > > > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > > -- > > 个人网站:http://zsp007.com.cn/ > > 专业:生物医学工程+计算机科学与技术 > > 技能:C++(STL,BOOST) Python(Django) HTML+CSS AJAX > > -- 张沈鹏 > > > > > -- > 个人网站:http://zsp007.com.cn/ > 专业:生物医学工程+计算机科学与技术 > 技能:C++(STL,BOOST) Python(Django) HTML+CSS AJAX > -- 张沈鹏 > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese
2007年10月20日 星期六 08:53
Python IAQ 是非常好的文档。看Peter Norvig这样的高手点评代码,就像看高手讲棋一样。享受! On 10/14/07, 张沈鹏(电子科大08年本科应届) <zsp007在gmail.com> wrote: > singleton参见 > http://www.chenwy.com/python-iaq-cn.html > http://wiki.woodpecker.org.cn/moin/SingletonPattern > > 在 07-10-14,张沈鹏(电子科大08年本科应届)<zsp007在gmail.com> 写道: > > class C(object): > > def __init__(self): self._x = None > > def getx(self): return self._x > > def setx(self, value): self._x = value > > def delx(self): del self._x > > x = property(getx, setx, delx, "I'm the 'x' property.") > > > > > > 在 07-10-14,pyman577<pyman577在sina.com> 写道: > > > 最近在看ruby,ruby实现singleton模式和类的属性都比较简单,比如 > > > class Singleton > > > private_class_method :new > > > @@instance = nil > > > def Singleton.instance > > > @@instance = new if ! @@instance > > > #or > > > #@@instance = new unless @@instance > > > end > > > def log(msg) > > > puts "instance write: #{msg}" > > > end > > > end > > > > > > 类的属性也很简单 > > > class CTest > > > @@index = 1 > > > attr :name,true > > > def initialize(name) > > > @name = name > > > end > > > 只要用attr关键字就可以很容易实现一个class的属性, > > > 我网上搜索了一下,python对应的方法相对比较复杂,有什么简单的方法吗?谢谢 > > > -- > > > > > > > > > _______________________________________________ > > > python-chinese > > > Post: send python-chinese在lists.python.cn > > > Subscribe: send subscribe to python-chinese-request在lists.python.cn > > > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > > -- > > 个人网站:http://zsp007.com.cn/ > > 专业:生物医学工程+计算机科学与技术 > > 技能:C++(STL,BOOST) Python(Django) HTML+CSS AJAX > > -- 张沈鹏 > > > > > -- > 个人网站:http://zsp007.com.cn/ > 专业:生物医学工程+计算机科学与技术 > 技能:C++(STL,BOOST) Python(Django) HTML+CSS AJAX > -- 张沈鹏 > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese
Zeuux © 2025
京ICP备05028076号