Object Oriented Programming in Python: self

Ji Kim·2021년 2월 3일
0

Algorithm Concept

목록 보기
3/6

Previously, we learned that the functions defined within the Class are methods and that we will assume that the first parameter of methods must be self. However, more precisely speaking, this assumption is wrong and we will be learning deeply about self.

Input

class Foo:
    def func1():
        print('Function 1')
    def func2(self):
        print('Function 2')

Although we have not passed self as the parameter of func1, class has been defined without returning error.

Now let us create instance f by calling Foo() and again call instance method func2().

Input

f = Foo()
f.func2()

Output

Function 2

Method func2() is printed. What would happen if we call func1() using same logic?

Input

f.func1()

Output

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-d7d514da12af> in <module>
----> 1 f.func1()

TypeError: func1() takes 0 positional arguments but 1 was given

As we have already expected, error occurs with the case of func1(). This is because Python method's first parameter always receives an instance.

Now, let us use Python's id method to check the address of the instance assigned to memory.

Input

class Foo:
    def func1():
        print('Function 1')
    def func2(self):
        print(id(self))
        print('Function 2')
f = Foo() 
id(f)

Output

140631691809936 # this will be different 

We can see that the instance f is located at memory's 140631691809936 index.

Instance of Class Foo() is assigned at memory's 140631691809936 index, and variable f contains the address of the instance.

Now, let us call method func2() using instance f

Input

f = Foo()
f.func2()

Output

140631691812048
Function 2

We see that both inputs return identical address line and realize that id(self) itself is 140631691809936. Therefore, self defined within the class is class instance.

In Python, class itself is a namespace allowing us to directly call class methods without creating instance (Explanation on namespace will follow).

Input

Foo.func1()

Output

Function 1

Since we called func1() by class.method(), error does not occur. What will happen if we call func2() with self parameter?

Input

Foo.func2()

Output

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-0047289629e6> in <module>
----> 1 Foo.func2()

TypeError: func2() missing 1 required positional argument: 'self'

Since we did not include a paramter to call fucn2(), an error message is returned. Then what should we pass instead?

We learned that self passed through method is instance itself. Therefore, we can create an instance of class and then pass that created instance as parameter.

Input

f3 = Foo()
id(f3)

Output

140631691810832

Input

Foo.func2(f3)

Output

140631691810832
Function 2

What if we access class method through instance?

Input

f3.func2()

Output

140631691810832
Function 2

This surely works too!

profile
if this then that

1개의 댓글

comment-user-thumbnail
2023년 6월 19일

The level of precision, attention to detail, and sheer artistry demonstrated in their work is remarkable. From intricate sculptures to sturdy industrial structures, this welding champ's creations are a testament to their exceptional craftsmanship. visit, click for more info, Use this website, click to read more, find more info.

답글 달기