0
class ABC:
    def xyz(self):
        print "in xyz"

obj = ABC()
print obj.xyz()

output : in xyz

here self is not given as parameter while calling xyz with obj.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437

4 Answers4

4

That's because self is, by default, the instance itself. obj.xyz() is equivalent to ABC.xyz(obj).

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
1

The first argument of every class method, including __init__, is always a reference to the current instance of the class.

By convention, this argument is always named self.

In the __init__ method, self refers to the newly created object; in other instance methods, it refers to the instance whose method was called. Although you need to specify self explicitly when defining the method, you do not specify it when calling the method; Python will add it for you automatically.

Learn more about self here and here

Community
  • 1
  • 1
Tanveer Alam
  • 5,185
  • 4
  • 22
  • 43
0

Technically, this is what happens:

  1. It tries to evaluate obj.xyz
  2. It funds that the object obj has no attribute named xyz
  3. It then looks in obj's class (ABC) for an attribute named xyz, which it does have
  4. ABC.xyz is a function, so then it "wraps" it into a "instance method". Basically, ABC.xyz is a function with one parameter (self), so the "instance method" is a function with one less parameter from the original function (no parameters in this case), which remembers the object obj. And if you call this "instance method", it passes it onto ABC.xyz, with the first argument being obj, the rest of the arguments being the argument to the bound method.
  5. obj.xyz evaluates to this instance method
  6. You make a call on this instance method, which calls ABC.xyz, with the remembered instance (obj) as the first argument.

Here are the relevant parts from the language reference (scroll from here):

Class instances

A class instance is created by calling a class object (see above). A class instance has a namespace implemented as a dictionary which is the first place in which attribute references are searched. When an attribute is not found there, and the instance’s class has an attribute by that name, the search continues with the class attributes. If a class attribute is found that is a user-defined function object, it is transformed into an instance method object whose __self__ attribute is the instance. [...]

and

Instance methods

[...]

When an instance method object is created by retrieving a user-defined function object from a class via one of its instances, its __self__ attribute is the instance, and the method object is said to be bound. The new method’s __func__ attribute is the original function object.

[...]

When an instance method object is called, the underlying function (__func__) is called, inserting the class instance (__self__) in front of the argument list. For instance, when C is a class which contains a definition for a function f(), and x is an instance of C, calling x.f(1) is equivalent to calling C.f(x, 1).

[...]

newacct
  • 119,665
  • 29
  • 163
  • 224
0

In the backend of the code obj.xyz(). The parameter returned is object(obj) itself that means ABC.xyz(obj)