Python: nested class with static method fails -
what wrong following code?
class a: def a_m(self): pass class b: @staticmethod def c(): super(b).a_m()
error (python 2.7.3):
>>> = a() >>> a.b.c() traceback (most recent call last): file "<stdin>", line 1, in <module> file "..x.py", line 36, in c def c(): super(b).a_m() nameerror: global name 'b' not defined
edit:
solution simple this:
class a: def a_m(self): pass class b: @staticmethod def c(): a().a_m() #use of a() instead of supper, etc.
important note there issue solution. if change name of super class (i.e. a
) have update uses inside a
:)).
class a(object): def foo(self): print('foo') @staticmethod def bar(): print('bar') class b(object): @staticmethod def bar(obj): # a.foo not staticmethod, can't use a.foo(), # need instance. # can't use super here a, # because b not subclass of a. obj.foo() a.foo(obj) # same obj.foo() # a.bar static, can use without object. a.bar() class b(a): def foo(self): # again, b.foo shouldn't staticmethod, because a.foo isn't. super(b, self).foo() @staticmethod def bar(): # have use super(type, type) if don't have instance. super(b, b).bar() a, b = a(), b() a.b.bar(a) b.foo() b.bar()
see this details on super(b, b)
.