method that can be called from a class and also from an instance
thbach at students.uni-mainz.de
Thu Nov 22 17:12:42 CET 2012
On Thu, Nov 22, 2012 at 10:52:56AM -0500, Dave Angel wrote:
> On 11/22/2012 10:14 AM, Marc Aymerich wrote:
> > I want to create a method within a class that is able to accept either a class or an instance.
> I haven't tried it, but how about if you do a @classmethod decorator,
> and then just use isinstance(param, MyClass) ?
This won't work:
In : class Foo(object):
....: def bar(cls):
....: print repr(cls)
In : Foo.bar()
In : Foo().bar()
Actually help(classmethod) explicitly says so:
It can be called either on the class (e.g. C.f()) or on an instance
(e.g. C().f()). The instance is ignored except for its class.
I think the way to go is via the descriptor protocol as suggested
More information about the Python-list