-1

Can anyone explain why when I create a class in Python for example:

class My:

and when I print(dir(python)) it returns a list of functions and attributes in which there is a __weakref__ method,

so my questions is from where did this method appear if the class that I create inherits from object class which doesn't have this method

azro
  • 53,056
  • 7
  • 34
  • 70
hal
  • 11

1 Answers1

0

These methods (the ones starting with double underscore) are called dunder methods.See more and this weakref

These are usually called by a wrapper function. For example, the dict() function calls the __dict__() method.

we won’t be needing the double underscore prefixed methods, so we can filter them using the below snippet:

method_list = [method for method in dir(My) if not method.startswith('__')]
print(method_list)

Output:

[]
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103