2024-09-30 03:25:09 发布
网友
可能是个愚蠢的问题,但我更习惯Java之类的东西,因此不明白为什么我可以这样做:
class A: def __init__( self ): pass a = A() a.description = "whyyy" print a.description
把它打印出来而不是给我一个错误。你知道吗
因为Python对象是动态的-它们不需要遵循严格的模式。你知道吗
创建一个类的实例会给您一个已经定义了某些内容的对象,但是您可以动态地向该实例添加其他内容;您不受原始类定义的限制。你知道吗
Variables spring into existence by being assigned a value, and they are automatically destroyed when they go out of scope.
对于对象,可以在运行时动态添加新字段。请注意,这不会更改类描述。仅当前实例。你知道吗
class A: def __init__( self ): pass a = A() a.description = "whyyy" print a.description b = A() print b.description # Should return an error
因为Python对象是动态的-它们不需要遵循严格的模式。你知道吗
创建一个类的实例会给您一个已经定义了某些内容的对象,但是您可以动态地向该实例添加其他内容;您不受原始类定义的限制。你知道吗
对于对象,可以在运行时动态添加新字段。请注意,这不会更改类描述。仅当前实例。你知道吗
相关问题 更多 >
编程相关推荐