Now I have to design a class that overload __getattr__, but after
that, I found the __repr__ have been affected. This is a simple
example model:
#!/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

So far, I want the operation "print t" still return "<test instance
at ...>", but the reuslt is:
sh$ python test.py
1
inexistent
<type 'instance'>
1
Traceback (most recent call last):
  File "testtree.py", line 23, in ?
    print t
TypeError: 'str' object is not callable

I also tried to overload __repr__ itself:

#!/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

So why? What is the principles?

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to