two new wrinkles to the general class!
Jeff Shannon
jeff at ccvcorp.com
Wed Nov 3 22:27:15 EST 2004
syd wrote:
>>>1) Because I've got many "container" type classes, the best route
>>>
>>>
>would obviously seem be to subclass each to a general "container". Ie,
>
>class Library_A(Container): ...
>class Library_B(Container): ...
>
>The problem: in the current setup, a library_a.get_continent('Europe')
>would pass back an instance of Container and *not* Library_A. The
>obvious implication is that any attributes/method specific to Library_A
>are gone.
>
As I understand it, your object's (most derived) type will be stored as
self.__class__. So, in order to have Library_A create more Library_A
instances, and Library_B create more Library_B instances, you can
instead do something like:
new_copy = self.__class__()
>2) I've got a bunch of "get_foo" type things where "foo" is not an
>
>
>attribute of the component class but rather a method. [...]
>
>Above, we use getattr() to grab the attribute on-the-fly. Can we grab
>a method on-the-fly, too?
>
Sure -- methods are just attributes that happen to be callable. You get
a method reference in the same way that you get any attribute, and you
call it the same way that you call any function reference:
method = Collection.get_foo('bar')
method()
will call whatever method get_foo('bar') returns.
Jeff Shannon
Technician/Programmer
Credit International
More information about the Python-list
mailing list