I've googled, but I couldn't find a solution to this. And I'm not even sure if this is "a Pythonic way to do it".
Let's take a simple example:
class Simple():
def __init__(self):
self.my_data = [1, 2, 3]
def __len__(self):
return len(self.my_data)
def new_method(self):
pass
# -> How to access the __len__() method here
How can I access the len()
method in new_method
(inside the class)?
CodePudding user response:
You can call len()
with an instance of Simple
as parameter
s = Simple()
print(len(s)) # 3
Inside the class you can use self
as the instance
def new_method(self):
print(len(self))