2007年01月22日 星期一 23:03
一个HTML附件被移除... URL: http://python.cn/pipermail/python-chinese/attachments/20070122/dbe4588a/attachment.html
2007年01月22日 星期一 23:11
On 1/22/07, brightman <fenyon在126.com> wrote: > > >>> class base: > ... i1=0 > ... i2=0 > ... > >>> b = base() > >>> for i in rang(1,3): > ... b."i%d" % i = i > > 当然我这个写法是错的,我只想用一个快捷的方法让b.i1=1 b.i2=2 。 > 因为可能类变量i1,i2,i3....很多,我想用一个格式化的字符串(或者其他非硬编码)来引用 类实例变量 > b.xxx 后面的xxx不能是一个表达式,如果xxx是动态算出来的,可以使用setattr(b, expression, value)来处理 -- I like python! UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad My Blog: http://www.donews.net/limodou
2007年01月23日 星期二 12:09
我是个新手,不过我觉得你这些变量用个字典或list不一下就可以实现你的这种操作了吗?
class base:
m = {}
b = base()
for I in rang(1,3):
id = "i%d"%(i)
b.m[id] = i
________________________________________
发件人: python-chinese-bounces在lists.python.cn [mailto:python-chinese-bounces在lists.python.cn] 代表 brightman
发送时间: 2007年1月22日 23:04
收件人: python-chinese在lists.python.cn
主题: [python-chinese] 如何动态地给类变量赋值
>>> class base:
... i1=0
... i2=0
...
>>> b = base()
>>> for i in rang(1,3):
... b."i%d" % i = i
当然我这个写法是错的,我只想用一个快捷的方法让b.i1=1 b.i2=2 。
因为可能类变量i1,i2,i3....很多,我想用一个格式化的字符串(或者其他非硬编码)来引用 类实例变量
2007年01月23日 星期二 13:12
setattr的参数应该是"setattr(object, name, value)"吧
楼主的代码可以这样写:
b = base()
for i in rang(1, 3):
setattr(b, "i"+i, i)
................
On 1/22/07, limodou <limodou在gmail.com> wrote:
>
> On 1/22/07, brightman <fenyon在126.com> wrote:
> >
> > >>> class base:
> > ... i1=0
> > ... i2=0
> > ...
> > >>> b = base()
> > >>> for i in rang(1,3):
> > ... b."i%d" % i = i
> >
> > 当然我这个写法是错的,我只想用一个快捷的方法让b.i1=1 b.i2=2 。
> > 因为可能类变量i1,i2,i3....很多,我想用一个格式化的字符串(或者其他非硬编码)来引用 类实例变量
> >
> b.xxx 后面的xxx不能是一个表达式,如果xxx是动态算出来的,可以使用setattr(b, expression, value)来处理
>
> --
> I like python!
> UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad
> My Blog: http://www.donews.net/limodou
> _______________________________________________
> 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
--
Best Regards,
Archer
Ming Zhe Huang
-------------- 下一部分 --------------
一个HTML附件被移除...
URL: http://python.cn/pipermail/python-chinese/attachments/20070123/904e9a6d/attachment.html
2007年01月23日 星期二 13:20
On 1/23/07, Mingzhe Huang <archerzz在gmail.com> wrote: > setattr的参数应该是"setattr(object, name, value)"吧 > > 楼主的代码可以这样写: > b = base() > for i in rang(1, 3): > setattr(b, "i"+i, i) > ................ > > > b.xxx 后面的xxx不能是一个表达式,如果xxx是动态算出来的,可以使用setattr(b, > expression, value)来处理 我写的只是一个示例。只是想突出 name 可以是一个表达式而已。不要被文字所迷惑啊,主要是理解意思。 -- I like python! UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad My Blog: http://www.donews.net/limodou
2007年01月23日 星期二 18:31
·Ç³£Ð»Ð»limodou.
ÿ´ÎÓÐÎÊÌ⣬¶¼µÃµ½ÄãÈÈÐĵĽâ´ð¡£
ÎҸоõµ½¶ÔpythonµÄ»ù±¾ÖªÊ¶»¹ºÜØÑ·¦£¬ËùÒÔ½ñÌì°Ñpython referenc·ÁËһϡ£
ÆäÖÐÓÐЩÎÊÌâÏëÇë½Ìһϴó¼Ò
ÎÒÓõÄÊÇpythonwin2.4
1.__new__×÷ΪÀàµÄ¾²Ì¬·½·¨£¬Ëü»áµ÷ÓÃ__init__()£¬²¢·µ»ØÊµÀý
>>> class C(object):
... def __new__(self):print "call C __new__"
... def __init__(self):print "call C __init__"
...
>>> x=C()
call C __new__ ûÓе÷ÓÃ__init__
>>> x
>>> print x
None
>>> isinstance(x,C)
False
>>>
2.__get__(self,instance,owner)
ÀàµÄdict°üº¬·½·¨Ê±£¬²Å¿ÉÒÔÓ¦ÓÃÕâЩ·½·¨
>>> class A:
... def __new__(cls):
... print "call class A __new__"
... def __init__(self):
... print "call class A __init__"
... def __del__(self):
... print "call class A __del__"
... def __lt__(self,other):
... return self.num < other.num
... def __nonzero__(self):
... return False
... def func1():print "func1"
...
>>> a=A()
>>> a.__dict__
{} ÕâÊÇÎÒÀí½âµÄÀàdictô£¿
>>> def fun(self):
... print "fun",self
...
>>> a.fun = fun.__get__(a,A)
>>> a.fun()
fun <__main__.A instance at 0x00F325F8>
ÎÒ¾õµÃºÜĪÃû£¬__get__ÕæÕýÓÐʲô×÷Óã¿
Brightman
2007-01-23
·¢¼þÈË£º limodou
·¢ËÍʱ¼ä£º 2007-01-23 13:21:24
ÊÕ¼þÈË£º python-chinese在lists.python.cn
³ËÍ£º
Ö÷Ì⣺ Re: [python-chinese]ÈçºÎ¶¯Ì¬µØ¸øÀà±äÁ¿¸³Öµ
On 1/23/07, Mingzhe Huang <archerzz在gmail.com > wrote:
> setattrµÄ²ÎÊýÓ¦¸ÃÊÇ"setattr(object, name, value)"°É
>
> Â¥Ö÷µÄ´úÂë¿ÉÒÔÕâÑùд£º
> b = base()
> for i in rang(1, 3):
> setattr(b, "i"+i, i)
> ................
>
> > b.xxx ºóÃæµÄxxx²»ÄÜÊÇÒ»¸ö±í´ïʽ£¬Èç¹ûxxxÊǶ¯Ì¬Ëã³öÀ´µÄ£¬¿ÉÒÔʹÓÃsetattr(b,
> expression, value)À´´¦Àí
ÎÒдµÄÖ»ÊÇÒ»¸öʾÀý¡£Ö»ÊÇÏëÍ»³ö name ¿ÉÒÔÊÇÒ»¸ö±í´ïʽ¶øÒÑ¡£²»Òª±»ÎÄ×ÖËùÃÔ»ó°¡£¬Ö÷ÒªÊÇÀí½âÒâ˼¡£
--
I like python!
UliPad < >: http://wiki.woodpecker.org.cn/moin/UliPad
My Blog: http://www.donews.net/limodou
_______________________________________________
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
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070123/5baa6aa7/attachment.html
2007年01月23日 星期二 19:12
On 1/23/07, Brightman <fenyon在126.com> wrote: > > > 非常谢谢limodou. > 每次有问题,都得到你热心的解答。 不用客气。 你问的都是python中比较难懂的问题,在平时的编程中很难用到,建议不明白就可以先不要研究了。 -- I like python! UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad My Blog: http://www.donews.net/limodou
2007年01月23日 星期二 19:35
>
> 1.__new__作为类的静态方法,它会调用__init__(),并返回实例
> >>> class C(object):
> ... def __new__(self):print "call C __new__"
> ... def __init__(self):print "call C __init__"
> ...
> >>> x=C()
> call C __new__ 没有调用__init__
> >>> x
> >>> print x
> None
> >>> isinstance(x,C)
> False
> >>>
>
x = C()
近似等价于:
x = C.__new__(C)
x.__init__()
但是你这里 C.__new__ 返回的是 None
2.__get__(self,instance,owner)
> 类的dict包含方法时,才可以应用这些方法
> >>> class A:
> ... def __new__(cls):
> ... print "call class A __new__"
> ... def __init__(self):
> ... print "call class A __init__"
> ... def __del__(self):
> ... print "call class A __del__"
> ... def __lt__(self,other):
> ... return self.num < other.num
> ... def __nonzero__(self):
> ... return False
> ... def func1():print "func1"
> ...
> >>> a=A()
> >>> a.__dict__
> {} 这是我理解的类dict么?
> >>> def fun(self):
> ... print "fun",self
> ...
> >>> a.fun = fun.__get__(a,A)
> >>> a.fun()
> fun <__main__.A instance at 0x00F325F8>
> 我觉得很莫名,__get__真正有什么作用?
>
类是 A ,而非 a 。
还是推荐我自己的blog:
http://codeplayer.blogspot.com/2006/12/python-method-function-descriptor.html
里面还有一些链接,你可以仔细看看。
swordsp 兄整理了纯粹python语言方面一些东西:
http://wiki.woodpecker.org.cn/moin/PythonLanguage
--
http://codeplayer.blogspot.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20070123/8e66d30a/attachment.html
2007年01月23日 星期二 21:28
好blog!已经收藏了。实在忍受不了msn了,我也把blog搬到blogger上了。 On 1/23/07, yi huang <yi.codeplayer在gmail.com> wrote: > > 1.__new__作为类的静态方法,它会调用__init__(),并返回实例 > > >>> class C(object): > > ... def __new__(self):print "call C __new__" > > ... def __init__(self):print "call C __init__" > > ... > > >>> x=C() > > call C __new__ 没有调用__init__ > > >>> x > > >>> print x > > None > > >>> isinstance(x,C) > > False > > >>> > > > > x = C() > 近似等价于: > x = C.__new__(C) > x.__init__() > 但是你这里 C.__new__ 返回的是 None > > 2.__get__(self,instance,owner) > > 类的dict包含方法时,才可以应用这些方法 > > >>> class A: > > ... def __new__(cls): > > ... print "call class A __new__" > > ... def __init__(self): > > ... print "call class A __init__" > > ... def __del__(self): > > ... print "call class A __del__" > > ... def __lt__(self,other): > > ... return self.num < other.num > > ... def __nonzero__(self): > > ... return False > > ... def func1():print "func1" > > ... > > >>> a=A() > > >>> a.__dict__ > > {} 这是我理解的类dict么? > > >>> def fun(self): > > ... print "fun",self > > ... > > >>> a.fun = fun.__get__(a,A) > > >>> a.fun() > > fun <__main__.A instance at 0x00F325F8> > > 我觉得很莫名,__get__真正有什么作用? > > > > 类是 A ,而非 a 。 > 还是推荐我自己的blog: > http://codeplayer.blogspot.com/2006/12/python-method-function-descriptor.html > 里面还有一些链接,你可以仔细看看。 > > swordsp 兄整理了纯粹python语言方面一些东西: > http://wiki.woodpecker.org.cn/moin/PythonLanguage > > -- > http://codeplayer.blogspot.com/ > _______________________________________________ > 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 > -- Best Regards, Archer Ming Zhe Huang -------------- 下一部分 -------------- 一个HTML附件被移除... URL: http://python.cn/pipermail/python-chinese/attachments/20070123/44985891/attachment.htm
Zeuux © 2025
京ICP备05028076号