Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using python 2.7 class variable as a function

I’m trying to use a python class variable as a function, using something like the following code:

class A(object):
    func = None
    @classmethod
    def class_init(c,f):
        c.func = f
    def go(self, p):
        A.func(p)

def my_print(p):
    print(p)

A.class_init(my_print)
a = A()
a.go('AAA')

When running it using python3, everything works as expected and AAA is printed.

Using python2, a TypeError exception is thrown:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

TypeError: unbound method my_print() must be called with A instance as first argument (got str instance instead)

It seems that python2 expects a class instance as if A.func was an object method.

What causes this different behavior between python2 and python3?

Is there a way to "tell" python to handle A.func as a non object method?

[I can think of walkarounds like saving A.func as a list, meaning c.func = [f] in class_init and later A.func[0](p) in go, but would like to understand the core reason for this behavior and whether there is a neat solution]

>Solution :

Since the function you want to add doesn’t take a class instance or a class as its first argument, apparently you’re adding a static method — so you have to explicitly tell Python that is what you are doing.

class A(object):
    func = None

    @classmethod
    def class_init(c, f):
        c.func = staticmethod(f)  # Assume function type.

    def go(self, p):
        A.func(p)

def my_print(p):
    print(p)

A.class_init(my_print)
a = A()
a.go('AAA')  -> AAA
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading