class NewStyle(object):
def __init__(self, name, deacription):
self.name = name
self.deacription = deacription
if __name__ == "__main__":
old = OldStyle('old', 'odl style class')
print old
print dir(old)
print type(old)
print "-----------------------------------------"
new = NewStyle('new', 'new style class')
print new
print dir(new)
print type(new)
有哪位大神可以帮我解释下每行代码是干嘛的吗
def __init__(self, name, deacription): # 类初始化,会在对象创建时自动执行这个函数下面的操作 self.name = name # 实例属性 self.deacription = deacription
class NewStyle(object): # 类定义语句
def __init__(self, name, deacription): # 类初始化,会在对象创建时自动执行这个函数下面的操作
self.name = name # 类属性
self.deacription = deacription # 类属性
if __name__ == "__main__": # 意思时直接允许这个.py文件就会执行下面的代码,如果是被其他文件import,则不会执行
old = OldStyle('old', 'odl style class') # 实例化这个类
print old
print dir(old) # 打印类的属性及方法
print type(old) #打印这个类的type
print "-----------------------------------------"
new = NewStyle('new', 'new style class')
print new
print dir(new)
print type(new)