Python Dunder或magic方法返回对象的类型()
对于许多事情都有dunder或magic函数,比如getPython Dunder或magic方法返回对象的类型(),python,python-3.x,Python,Python 3.x,对于许多事情都有dunder或magic函数,比如getlen()、print(),我们甚至可以使用help(),而无需使用magic函数,如果我们使用了正确的docstring,就可以使用?Class 假设我有一个这样的类: class Node(): def __init__(self, data): self.data = data self.next = None def __repr__(self): pass
len()、print()
,我们甚至可以使用help()
,而无需使用magic函数,如果我们使用了正确的docstring,就可以使用?Class
假设我有一个这样的类:
class Node():
def __init__(self, data):
self.data = data
self.next = None
def __repr__(self):
pass
def __str__(self):
pass
def __len__(self):
pass
node = Node([1,2,3])
type(node)
当我使用type()
时,它说的是\uuuu主节点
。我必须使用什么神奇的方法来返回像LinkedList.Node
或simpleNode
??就像它返回list
,str
或numpy.ndarray
?type()
不调用任何神奇的方法。它只返回instance.\uuuuu class\uuuu
属性的值
手动覆盖它不一定是个好主意。如果您想要LinkedList.Node
而不是\uuuuu main\uuuu Node
,最好的办法是在LinkedList
模块内定义节点。type()
不调用任何魔法方法。它只返回instance.\uuuuu class\uuuu
属性的值
手动覆盖它不一定是个好主意。如果您想要LinkedList.Node
而不是\uuuuu main\uuuu Node
,最好的办法是在LinkedList
模块中定义节点