Python论坛  - 讨论区

标题:[python-chinese] why __repr__ affected after __getattr__ overloaded?

2007年06月22日 星期五 12:03

Roc Zhou chowroc.z+l在gmail.com
星期五 六月 22 12:03:42 HKT 2007

ÎÒÏÖÔÚÒòΪÐèÒªÉè¼ÆÒ»¸öÀ࣬ÆäÖÐ __getattr__ ¿ÉÄÜÐèÒªÖØÔØ£¬µ«ÖØÔغóÎÒ·¢ÏÖ __repr__ ±»Ó°ÏìÁË¡£ÕâÀïÊÇÒ»¸ö¼òµ¥µÄÄ£ÐÍ£º

#!/usr/bin/env python

class test:
    def __init__(self):
        self.x = 1
    def __getattr__(self, attr_name):
        try:
            return self.__dict__[attr_name]
        except KeyError:
            self.__dict__[attr_name] = 'inexistent'
            return self.__dict__[attr_name]

t = test()
print t.x
print t.y
print type(t)
T = t
print T.x
print t

µ½Ä¿Ç°ÎªÖ®£¬ÎÒÈÔȻϣÍû "print t" Äܹ»·µ»Ø ""£¬µ«ÊÇÔËÐнá¹ûÈ´ÊÇ£º
sh$ python test.py
1
inexistent

1
Traceback (most recent call last):
  File "testtree.py", line 23, in ?
    print t
TypeError: 'str' object is not callable

ÎÒÒ²³¢ÊÔÁËÖØÔØ __repr__£¬µ«Òì³£ÒÀ¾É£º

#!/usr/bin/env python

class test:
    def __init__(self):
        self.x = 1
    def __getattr__(self, attr_name):
        try:
            return self.__dict__[attr_name]
        except KeyError:
            self.__dict__[attr_name] = 'inexistent'
            return self.__dict__[attr_name]
    def __repr__(self):
        return 'test.__repr__'

t = test()
print t.x
print t.y
print type(t)
T = t
print T.x
print t

But the result remains:
Traceback (most recent call last):
  File "testtree.py", line 23, in ?
    print t
TypeError: 'str' object is not callable

ÄÇôԭÒòºÎÔÚÄØ£¿

лл¡£
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070622/2cc38d20/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年06月22日 星期五 14:25

Liming_Do Liming_Do在smics.com
星期五 六月 22 14:25:31 HKT 2007

ÄãÕâÖÖÇé¿öÒªÖØÔØ__str__
 
#!/usr/bin/env python
 
class test:
    def __init__(self):
        self.x = 1
    def __getattr__(self, attr_name):
        try:
            return self.__dict__[attr_name]
        except KeyError:
            self.__dict__[attr_name] = 'inexistent'
            return self.__dict__[attr_name]
 
    def __str__(self):
        return 'test.__str__'
    
if __name__ == '__main__':
    t = test()
    print t.x
    print t.y
    print type(t)
    T = t
    print T.x
    print t
 
>>> 
1
inexistent

1
test.__str__
>>> 

-----Original Message-----
From: python-chinese-bounces在lists.python.cn [mailto:python-chinese-bounces在lists.python.cn]On Behalf Of Roc Zhou
Sent: Friday, June 22, 2007 12:04 PM
To: python-chinese在lists.python.cn
Subject: [python-chinese] why __repr__ affected after __getattr__ overloaded?


ÎÒÏÖÔÚÒòΪÐèÒªÉè¼ÆÒ»¸öÀ࣬ÆäÖÐ __getattr__ ¿ÉÄÜÐèÒªÖØÔØ£¬µ«ÖØÔغóÎÒ·¢ÏÖ __repr__ ±»Ó°ÏìÁË¡£ÕâÀïÊÇÒ»¸ö¼òµ¥µÄÄ£ÐÍ£º

#!/usr/bin/env python

class test:
    def __init__(self):
        self.x = 1
    def __getattr__(self, attr_name):
        try:
            return self.__dict__[attr_name]
        except KeyError:
            self.__dict__[attr_name] = 'inexistent'
            return self.__dict__[attr_name] 

t = test()
print t.x
print t.y
print type(t)
T = t
print T.x
print t

µ½Ä¿Ç°ÎªÖ®£¬ÎÒÈÔȻϣÍû "print t" Äܹ»·µ»Ø ""£¬µ«ÊÇÔËÐнá¹ûÈ´ÊÇ£º
sh$ python test.py
1
inexistent

1
Traceback (most recent call last):
  File "testtree.py", line 23, in ?
    print t
TypeError: 'str' object is not callable 

ÎÒÒ²³¢ÊÔÁËÖØÔØ __repr__£¬µ«Òì³£ÒÀ¾É£º

#!/usr/bin/env python

class test:
    def __init__(self):
        self.x = 1
    def __getattr__(self, attr_name):
        try:
            return self.__dict__[attr_name] 
        except KeyError:
            self.__dict__[attr_name] = 'inexistent'
            return self.__dict__[attr_name]
    def __repr__(self):
        return 'test.__repr__'

t = test() 
print t.x
print t.y
print type(t)
T = t
print T.x
print t

But the result remains:
Traceback (most recent call last):
  File "testtree.py", line 23, in ?
    print t
TypeError: 'str' object is not callable 

ÄÇôԭÒòºÎÔÚÄØ£¿

лл¡£


-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070622/67f801f8/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年06月22日 星期五 15:32

Roc Zhou chowroc.z+l在gmail.com
星期五 六月 22 15:32:43 HKT 2007

лл£¬Õâ¸öÎÊÌâÎÒ¸Õ²ÅŪÃ÷°×ÁË£¬µ«ÊÇÓÖÓÐÁíÍâÒ»¸öÎÊÌâÁË£º

>>> class test:
...     def __init__(self):
...         self.x = 1
...     def __getattr__(self, attr_name):
...         print attr_name
...         if attr_name == 'y':
...             return 2
...         else:
...             raise AttributeError, attr_name
...
>>> t = test()
>>> t.x
1
>>> t.y
y
2
>>> print t.x
1
>>> print t
__str__
__repr__
<__main__.test instance at 0xb7f6d6cc>

__str__ ºÍ __repr__ µÄ attr_name ÒѾ­±»´òÓ¡³öÀ´£¬ËµÃ÷ __getattr__ ÔÚ print
µÄʱºò±»µ÷ÓÃÁË£¬²¢ÇÒËüÓÖ²»ÊÇ "y"£¬ÎªÊ²Ã´Ã»ÓÐÅ׳ö AttributeError µÄÒì³££¿


On 6/22/07, Liming_Do <Liming_Do在smics.com> wrote:
>
>  ÄãÕâÖÖÇé¿öÒªÖØÔØ__str__
>
> #!/usr/bin/env python
>
> class test:
>     def __init__(self):
>         self.x = 1
>     def __getattr__(self, attr_name):
>         try:
>             return self.__dict__[attr_name]
>         except KeyError:
>             self.__dict__[attr_name] = 'inexistent'
>             return self.__dict__[attr_name]
>
>     def __str__(self):
>         return 'test.__str__'
>
> if __name__ == '__main__':
>     t = test()
>     print t.x
>     print t.y
>     print type(t)
>     T = t
>     print T.x
>     print t
>
> >>>
> 1
> inexistent
> 
> 1
> test.__str__
> >>>
>
> _______________________________________________
> 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/20070622/040d9c83/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年06月22日 星期五 15:44

limodou limodou在gmail.com
星期五 六月 22 15:44:31 HKT 2007

这应该是print处理了异常。

-- 
I like python!
UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad
My Blog: http://www.donews.net/limodou

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年06月22日 星期五 16:09

Liming_Do Liming_Do在smics.com
星期五 六月 22 16:09:36 HKT 2007

偶觉得似乎不是这个原因
而是这兄弟并未引用一个不存在的成员,哪里会有异常抛出来呢

class test:
    def __init__(self):
        self.x = 1
    def __getattr__(self, attrname):
        print attrname
        if attrname == 'y':
            return 2
        else:
            raise AttributeError, attrname

if __name__=='__main__':
    t = test()
    print t.x
    print t.y
    print t.z
    print t

>>> 
1
y
2
z

Traceback (most recent call last):
  File "D:/LeonDong/StudyStudio/Python/getattr/test2.py", line 15, in 
    print t.z
  File "D:/LeonDong/StudyStudio/Python/getattr/test2.py", line 9, in __getattr__
    raise AttributeError, attrname
AttributeError: z
>>> 

-----Original Message-----
From: python-chinese-bounces在lists.python.cn
[mailto:python-chinese-bounces在lists.python.cn]On Behalf Of limodou
Sent: Friday, June 22, 2007 3:45 PM
To: python-chinese在lists.python.cn
Subject: Re: [python-chinese] why __repr__ affected after
__getattr__overloaded?


这应该是print处理了异常。

-- 
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

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号