【问题标题】:Difference between 'class MyClass:' and 'class MyClass():' [duplicate]'class MyClass:'和'class MyClass()之间的区别:' [重复]
【发布时间】:2020-01-14 11:32:07
【问题描述】:

带括号和不带括号,有区别吗?

class Host:
def __init__(self, ip, elements):

class Interface():
def __init__(self, localIP, remoteIP, numHops, hops):

【问题讨论】:

标签: python oop


【解决方案1】:

不,没有任何区别,任何半体面的 IDE 都会建议您“删除多余的括号”。

在 Python 2 天中,class Hostclass Host(object) 之间存在差异(请参阅 What is the difference between old style and new style classes in Python?)。

在 Python 3 中,class Hostclass Host(object)class Host() 都是等价的。

【讨论】:

    【解决方案2】:

    通常,类定义中的括号用于表示继承

    class Interface(baseClass):
    

    如果你的类没有从另一个对象继承任何属性,那么省略括号更自然:

    class Interface:
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-09
      • 2016-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-28
      相关资源
      最近更新 更多