Jakiś problem?

Self w Pythonie

Self in python represents or points the instance which it was called. 

obj1 = SomeClass()
obj2 = SomeClass()
obj1.insert_to_arr(6)

But now how does that method know “which object is calling me and whose instance attributes should be updated”. Here, to whose arr array should I append the value 6

Behind the scene, in every instance method call, python sends the instance also with that method call. So what actually happens is, python convert the above calling of the instance method to something like below:

SomeClass.inseart_to_arr(obj1, 6)

Źródło:
https://medium.com/quick-code/understanding-self-in-python-a3704319e5f0

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany. Wymagane pola są oznaczone *