Creating a method that is simultaneously an instance and class method
Posted
by Mike Axiak
on Stack Overflow
See other posts from Stack Overflow
or by Mike Axiak
Published on 2010-04-07T02:45:09Z
Indexed on
2010/04/07
2:53 UTC
Read the original article
Hit count: 253
In Python, I'd like to be able to create a function that behaves both as a class function and an instance method, but with the ability to change behaviors. The use case for this is for a set of serializable objects and types. As an example:
>>> class Thing(object):
#...
>>> Thing.to_json()
'A'
>>> Thing().to_json()
'B'
I know that given the definition of classmethod() in funcobject.c in the Python source, this looks like it'd be simple with a C module. Is there a way to do this from within python?
Thanks!
© Stack Overflow or respective owner