Re: Unexpected __metaclass__ method behavior
- From: Arnaud Delobelle <arnodel@xxxxxxxxxxxxxx>
- Date: Mon, 31 Dec 2007 10:32:44 -0800 (PST)
On Dec 31, 12:06 pm, anne.nospa...@xxxxxxxxxxx wrote:
Well, you see, I have some database functions that deal with "things"
which are either classes or instances thereof. I though polymorphism
would be a nice way to handle them identically, like:
def do(thing): thing.Foo()
do(t)
do(Test)
But never mind, I now understand that Test.__dict__ can contain only
one entry for 'Foo', and that this must be matched.
Kind regards,
Sebastian
Of course you can do this. The trick is *not* to use metaclasses!
class Bar(object):
def foo(self): return 'instance foo'
@classmethod
def classfoo(cls): return 'class foo'
def do(x):
if isinstance(x, type):
return x.classfoo()
else:
return x.foo()
Then:
'instance foo'bar = Bar()
do(bar)
'class foo'do(Bar)
HTH
--
Arnaud
.
- References:
- Unexpected __metaclass__ method behavior
- From: anne . nospam01
- Re: Unexpected __metaclass__ method behavior
- From: Terry Reedy
- Re: Unexpected __metaclass__ method behavior
- From: anne . nospam01
- Unexpected __metaclass__ method behavior
- Prev by Date: Re: os.fork leaving processes behind
- Next by Date: Re: What's the limit of variables size in pyhton?
- Previous by thread: Re: Unexpected __metaclass__ method behavior
- Next by thread: What is the best way to do dynamic imports ?
- Index(es):
Relevant Pages
|