声明A类和A()类有什么区别? [英] What's the difference between declaring `class A` and `class A()`?
本文介绍了声明A类和A()类有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们在python中定义类如下:
We define class in python as following:
class A:
def x(self):
print("In x")
但是我只是打错了字,它在python 3.7版的linux cli中成功执行了.在这里:
But i just made a typo and it executed successfully in linux cli with python version 3.7. Here it is:
class A():
def x(self):
print("In x")
这是什么意思?
推荐答案
您可以选择继承在Python中使用语法 class Child(Parent):
从其他类中获取.括号中不留任何参数都与括号中留有相同:您只是从基类 object
继承而已,而没有别的什么.例如:
You can optionally inherit from other classes with the syntax class Child(Parent):
in Python. Leaving no argument in the parenthesis is the same as leaving the parenthesis off: you just inherit from the base object
class and nothing else. For example:
class A:
pass
class B(object):
pass
class C():
pass
import inspect
inspect.getmro(A)
# (__main__.A, object)
inspect.getmro(B)
# (__main__.B, object)
inspect.getmro(C)
# (__main__.C, object)
所有等效项.作为反例:
All equivalent. As a counter-example:
class D(A): # inherit from A
pass
inspect.getmro(D) # should include A now
# (__main__.D, __main__.A, object)
这篇关于声明A类和A()类有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文