Re: two new wrinkles to the general class!
syd wrote:
[color=blue][color=green][color=darkred]
>>>1) Because I've got many "container" type classes, the best route
>>>
>>>[/color][/color]
>would obviously seem be to subclass each to a general "container" . Ie,
>
>class Library_A(Conta iner): ...
>class Library_B(Conta iner): ...
>
>The problem: in the current setup, a library_a.get_c ontinent('Europ e')
>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.
>[/color]
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__( )
[color=blue]
>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?
>[/color]
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
syd wrote:
[color=blue][color=green][color=darkred]
>>>1) Because I've got many "container" type classes, the best route
>>>
>>>[/color][/color]
>would obviously seem be to subclass each to a general "container" . Ie,
>
>class Library_A(Conta iner): ...
>class Library_B(Conta iner): ...
>
>The problem: in the current setup, a library_a.get_c ontinent('Europ e')
>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.
>[/color]
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__( )
[color=blue]
>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?
>[/color]
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
Comment