wobbily_col
wobbily_col

Reputation: 11921

Are all python magic methods available to all objects

Am I correct in assuming that an object in Python has a default implementation of all magic methods (the ones surrounded by the double underscore, such as __init__)?

Upvotes: 3

Views: 213

Answers (2)

user2357112
user2357112

Reputation: 281653

No.

>>> object.__add__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'object' has no attribute '__add__'

Some of them do have default implementations, like __init__, but most of them don't.

Upvotes: 6

Arovit
Arovit

Reputation: 3709

No it does not have default implementations of all the magic functions.

It would be only true if all the objects were inherited from a common default base class.

There are different classes and different implementation of these classes require different magic functions.

Do object.__class__ to get the class name

To find out all the functions in that object you can do dir(object)

More - http://docs.python.org/release/2.5.2/ref/node33.html

Upvotes: -1

Related Questions