general class functions

Dan Perl danperl at rogers.com
Tue Nov 2 20:52:18 CET 2004


"syd" <syd.diamond at gmail.com> wrote in message 
news:a76ba315.0411021009.46b06459 at posting.google.com...
> Many many thanks, Andrea and Kent!
>
> To keep changes to calls in my current code to a minimum, I'm hoping I
> can encode Andrea's "on-the-fly" get function.  I added Andrea's
> __getattr__ to my Library() class, populated the class with a few
> nations, and tried:
>
>>>> library.get_continent('Europe')

This line creates and returns an instance of Library.  I assume that you 
imported foo in a python session and then you executed that line.  That 
results in a call to Library.__repr__( ), which is not defined.  First of 
all, I recommend you change class Library to subclass from object.  That's 
just good practice in general and in this case it will give you a default 
__repr__( ) method.  But that's probably not what you want.  What you 
probably want is to still use a line like:
    print library.getContinent('Europe').getNameList()
Otherwise, library.get_continent('Europe') is going to print something like:
    <__main__.Library object at 0x00A9F430>

Dan

> Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
>  File "foo.py", line 16, in __getattr__
>    return self.__dict__[name]
> KeyError: '__repr__'
>
> I then commented out lines 15 and 16...
>    #else:
>      #return self.__dict__[name]
>
> ... and re-ran.
>
>>>> library.get_continent('Europe')
> Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
> TypeError: 'NoneType' object is not callable
>
> I tweaked a bit, but I could not get to the bottom of this problem.  I
> see the logic here (and it's brilliant I should add, Andrea), but I'm
> not sure what is wrong.
>
> Ideas? 





More information about the Python-list mailing list