类的方法名称可以赋值为数字?

谢谢!

class C:
def x (self):
print ("你好!")

c=C()
c.x = 1 #这是什么操作?

1、操作解释
其实就是将定义的方法自动转为了类的一个普通变量,method转为了int
所以,你会发现c.x(),再这样子使用就会报错,此时已经是一个整型,不能当作方法使用

img

2、运行代码

class C:
    def x (self):
        print ("你好!")
    def y (self):
        print ("你好!%s" % self.x)

c=C()
print ("方法被赋值整型值前的类型:%s" % type(c.x))
c.x = 1
print ("方法被赋值整型值后的类型:%s" % type(c.x))
c.y()

c.x = 666
c.y()

不能以数字为开头


print(type(c.x))

<class 'int'>

你这里这么用不是给方法赋值,而是创建了名为x的内部变量