1

I have been learning python and currently learning OOP python and saw this on W3 schools but dont understand how it works

class Person:
  def __init__(mysillyobject, name, age):
    mysillyobject.name = name
    mysillyobject.age = age

  def myfunc(abc):
    print("Hello my name is " + abc.name)

p1 = Person("John", 36)
p1.myfunc()

Doesn't the method have to reference current instance of the class to access variables of class so confused as to why the method can use abc and not my sillyobject and be able to compile. Any explanation would be so helpful. Sorry if this is really obvious am newish to python

Alexander
  • 59,041
  • 12
  • 98
  • 151

1 Answers1

2

The name of the argument variable in the method does not matter to python.

Python will always put the reference of the current instance into the first of the method arguments. Using self is just a convention and it is just an ordinary variable name. However it's a very well accepted convention so please use self unless you are trying to highlight a point with code such as in the W3 schools page.

One.Punch.Leon
  • 602
  • 3
  • 10