慕函数8028368
2018-12-05 08:20
为什么执行老师的程序,class OldStyle: def _init_(self,name,description): self.name=name self.description=description class NewStyle(object): def _init_(self,name,description): self.name=name self.description=description if __name__=='_main_': old=OldStyle('old','OldStyle') print(old) print (type(old)) print(dir(old)) print('-------------------------------------') new=NewStyle('new','NewStyle') print (type(new)) print(dir(new))
编译成功,却没结果
class OldStyle:
#这里__init__是两下划线哦
def __init__(self, name, description):
self.name = name
self.description = description
class NewStyle(object):
#这里也__init__是两下划线哦
def __init__(self, name, description):
self.name = name
self.description = description
# 这里是 '__main__' 是两下划线哦 因为:__name__ == '_main_' 是不相等,所以这个if下的代码就没在执行
if __name__ == '__main__':
old = OldStyle('old', 'OldStyle')
print(old)
print(type(old))
print(dir(old))
print('-------------------------------------')
new = NewStyle('new', 'NewStyle')
print(type(new))
print(dir(new))
Python-面向对象
71236 学习 · 81 问题
相似问题
回答 3
回答 1