问答详情
源自:3-1 用Python定义类

输入结果有误

为什么执行老师的程序,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))

编译成功,却没结果

提问者:慕函数8028368 2018-12-05 08:20

个回答

  • 沐枫自然
    2018-12-05 17:40:59

    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))