-2

Can this code

class Point:
    def __init__(self,x,y):
        self.x=x
        self.y=y
    def __str__(self):
        return "Point({},{})".format(self.x,self.y)
p=Point(3,5)
print(p)

be modified to following code?

class Point:
    def __init__(p,x,y):
        p.x=x
        p.y=y
    def __str__(p):
        return "Point({},{})".format(p.x, p.y)
p=Point(3,5)
print(p)

It seems to be worked in this case. But it is so naive case. I wonder using other than 'self' will cause some problem in some situations.

hoge1e3
  • 802
  • 5
  • 15
  • See https://docs.python.org/3/tutorial/classes.html#random-remarks – jonrsharpe Jun 02 '20 at 06:49
  • 1
    Does this answer your question? [Why do you need explicitly have the "self" argument in a Python method?](https://stackoverflow.com/questions/68282/why-do-you-need-explicitly-have-the-self-argument-in-a-python-method) – Priyank-py Jun 02 '20 at 07:03

1 Answers1

0

The first argument is the reference to the bound variable or object. Its convention to use self but anything else will also work. check the doc here.

Priyank-py
  • 349
  • 3
  • 14