1
class A(object):
    def __nikunj__(self):
        print("Inside :: A")
    def __nike__(self):
        print("Something")

class B(A):

    def __nike__(self):
        print("inside :: B")
        super(B,self).__nike__()


object_B = B()
print(object_B.__nike__())

Above code works fine.

class A(object):
    def __nikunj__(self):
        print("Inside :: A")
    def __nike__(self):
        print("Something")

class B(A):
    test = super(B,self).__nike__()
    def __nike__(self):
        print("inside :: B")



object_B = B()
print(object_B.test)

This code throws Error :

    Traceback (most recent call last):
    File "C:\Users\Nikunj Parmar\AppData\Local\Programs\Python\Python36               \test_test.py", line 7, in <module>
    class B(A):
    File "C:\Users\Nikunj Parmar\AppData\Local\Programs\Python\Python36\test_test.py", line 8, in B
    test = super(B,self).__nike__()
    NameError: name 'B' is not defined

Why? I don't understand why it shows that B is not found (in second case).

Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

1
  1. Methods are called (ie executed) after an object exists (ie the class itself or an instance of it).

    In the 1st example by the time __nike__ is called the B class is already defined. Statements in a class body are executed when in the class's definition time. In the 2nd example, by the time test = super(B,self).__nike__() is executed there is no such thing as class B since this line is executed when the class is defined for the very first time.

    The same behavior can be observed in the following line: d = {'a': 1, 'b': d['a']}. This results with NameError: name 'd' is not defined since d is not yet defined when we attempt to access d['a'].

  2. Don't use dunder names for arbitrary methods.

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
1

When the class body is being evaluated, the class object has not been created yet.

According to the docs:

When a class definition is entered, a new namespace is created, and used as the local scope — thus, all assignments to local variables go into this new namespace. In particular, function definitions bind the name of the new function here.

When a class definition is left normally (via the end), a class object is created. This is basically a wrapper around the contents of the namespace created by the class definition; we’ll learn more about class objects in the next section. The original local scope (the one in effect just before the class definition was entered) is reinstated, and the class object is bound here to the class name given in the class definition header (ClassName in the example).

You may want to read that whole page. It explains exactly how the namespace of a class works, and when things get created.

The first version works because of LEGB evaluation. Your method code is evaluated but not called when the method is defined. Using B within a method compiles to "find name B". The name does not have to exist until you actually call the method. When you do finally execute the code, the names A and B are already bound to classes in the global namespace (the G in LEGB), and everything works out fine.

Finally, keep in mind that using double underscore (dunder) methods (a.k.a. special methods or magic methods) is extremely frowned upon in Python. These methods serve the purpose of operator overloading, and all such names are automatically reserved. From the docs:

Any use of __*__ names, in any context, that does not follow explicitly documented use, is subject to breakage without warning.

Since you are new to Python, I have attempted to post some links for you to follow, as well as mentioning some basic concepts that are pretty ubiquitous in the language. Hope that helps you in the future.

Community
  • 1
  • 1
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264