python self-less

后端 未结 3 1793
一个人的身影
一个人的身影 2021-01-22 05:03

this works in the desired way:

class d:
    def __init__(self,arg):
        self.a = arg
    def p(self):
        print \"a= \",self.a

x = d(1)
y = d(2)
x.p()
y         


        
3条回答
  •  故里飘歌
    2021-01-22 05:28

    When you remove the self's, you end up having only one variable called a that will be shared not only amongst all your d objects but also in your entire execution environment. You can't just eliminate the self's for this reason.

提交回复
热议问题