why __repr__ affected after __getattr__ overloaded?

Roc Zhou chowroc.z at gmail.com
Fri Jun 22 01:56:42 EDT 2007


return hex(id(self))

On 6 22 ,   1 48 , Roc Zhou <chowro... at gmail.com> wrote:
> I know what's wrong. Thank you. And I think
> try:
>     return self.__dict__[attr_name]
> is unnecessary, because python will do it itself for us.
>
> So now I have to overload __str__, but how can I make self.__str__
> print as builtin str(): at here, I want get the result like:
> <test instance at 0xb7bbb90c>
> ?
>
> On 6 22 ,   12 55 , "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
> wrote:
>
> > En Fri, 22 Jun 2007 00:30:43 -0300, Roc Zhou <chowro... at gmail.com>
> > escribió:
>
> > > 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:
>
> > You are creating many attributes with value "inexistent", even special
> > methods. Put a print statement and see what happens:
>
> > > #!/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:
>
> >                print "Now creating:",attr_name
>
> > >             self.__dict__[attr_name] = 'inexistent'
> > >             return self.__dict__[attr_name]
>
> > --
> > Gabriel Genellina







More information about the Python-list mailing list