0

What is wrong with the 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()
>>> 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' is not defined

Edit:
the solution was simple as 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 that there is an issue with this solution. If you change the name of super class (i.e. A) then you will have to update all uses inside itself as A :)).

2
  • Why do you think you want a nested class? There is almost zero reason to ever have this in Python. Commented Apr 25, 2013 at 8:52
  • " If you change the name of super class (i.e. A) then you will have to update all uses inside itself as A" - That's not what super class means Commented Apr 25, 2013 at 21:02

3 Answers 3

3
class A(object):
    def foo(self):
        print('foo')

    @staticmethod
    def bar():
        print('bar')

    class B(object):
        @staticmethod
        def bar(obj):
            # A.foo is not staticmethod, you can't use A.foo(),
            # you need an instance.
            # You also can't use super here to get A,
            # because B is not subclass of A.
            obj.foo()
            A.foo(obj)  # the same as obj.foo()

            # A.bar is static, you can use it without an object.
            A.bar()

class B(A):
    def foo(self):
        # Again, B.foo shouldn't be a staticmethod, because A.foo isn't.
        super(B, self).foo()

    @staticmethod
    def bar():
        # You have to use super(type, type) if you don't have an instance.
        super(B, B).bar()


a, b = A(), B()

a.B.bar(a)
b.foo()
B.bar()

See this for details on super(B, B).

Sign up to request clarification or add additional context in comments.

1 Comment

the solution is therefore: class A: def A_M(self): pass class B: @staticmethod def C(): A().A_M()
2

You need to use a fully-qualified name. Also, in python 2.7, you need to use (object), else super(A.B) will give TypeError: must be type, not classobj

class A(object):
    def A_M(self):
        pass

    class B(object):
        @staticmethod
        def C():
            super(A.B).A_M()

Finally, super(A.B) is essentially object here. Did you mean for B to inherit from A? Or were you simply looking for A.A_M()?

6 Comments

doesn't work: ...error: def C(): super(A.B).A_M() TypeError: must be type, not classobj
@Developer: Ok, now read my post again: "Also, in python 2.7, you need to use (object)". super only works on new-style classes
still doesn't work: ...err: super(A.B).A_M() AttributeError: 'super' object has no attribute 'A_M'
@Developer: Keep reading - "super(A.B) is essentially object here". And object.A_M() doesn't exist. "Did you mean for B to inherit from A? Or were you simply looking for A.A_M()?"
Even if it was inherited, it still don't think it would work because it's static
|
2

A latecommer, to just encapsulate B in A the easy way is this:

class A:
    def A_M(self):
        return "hi"

    class B:
        @staticmethod
        def C():
            return A().A_M()

a = A()
print a.B().C()

Not sure this is what you need, but the question was still unsolved, so I guessed.

1 Comment

a.B().C() is not correct. a.B.C() is correct (because C is static in B). I voted up your answer, BTW. gatto's answer says the same thing but a little lengthy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.