简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM