Reputation: 4152
There are dunder or magic functions for many things like to get len(), print()
we can even use help()
without even using a magic function and use ?Class
if we have used the proper docstring.
Suppose I have a class like this:
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)
When I used the type()
, it said __main__Node
. What magic method I have to use it to return something like LinkedList.Node
or simple Node
?? Just like it returns list
, str
or numpy.ndarray
??
Upvotes: 1
Views: 573
Reputation: 81594
type()
does not call any magic method. It merely returns the value of instance.__class__
attribute.
Manually overwriting it is not necessarily a good idea. If you want LinkedList.Node
instead of __main__Node
the best thing to do is to define Node
inside a LinkedList
module.
Upvotes: 3