Discuss / Python / 我想问下这两句的区别在哪里呢

我想问下这两句的区别在哪里呢

Topic source

稻草人↓

#1 Created at ... [Delete] [Delete and Lock User]
def fn(self, name='world'): # 先定义函数
     print('Hello, %s.' % name)
Hello = type('Hello', (object,), dict(hello=fn))
NoHello = type('Hello', (object,), dict(hello=fn))

主要想问type里面的Hello有什么用,貌似跟作者解释的类的名称没有什么关系呢,随便写个参数,等于右边的时候那个才是类的名称。

Yee

#2 Created at ... [Delete] [Delete and Lock User]

区别就是type里面的Hello就是定义的类名(即__name__属性返回的值),而等号左边的是调用类时候用到的类似变量一样的东西,两个保持一致主要是为了严谨

lucky lucy

#3 Created at ... [Delete] [Delete and Lock User]

本质上没差别吧 hello() 和 nohello()创建的是同一类hello的实例


  • 1

Reply