etwinomujuni
etwinomujuni

Reputation: 3

Can I assign a member function to a local variable in a python class

I have a class;

class Foo:
    bar = fooFunc

    def fooFunc():
        return True

The linter complains: fooFunc is not defined. What could I be missing. Thanks

Upvotes: 0

Views: 77

Answers (1)

jthulhu
jthulhu

Reputation: 8678

This seems logical: the code gets executed sequentially. If you want to use fooFunc, you have to define it first! Simply try

class Foo:
  def fooFunc():
    pass
  bar = fooFunc

Also, note that a function defined in a class by default will be a method, so it should accept at least self. If you just want to put it there for namespacing reasons, you should make it a staticmethod instead.

Upvotes: 1

Related Questions