views:

123

answers:

1

What is the magic method? Please give brief definition of magic method?

+2  A: 

Magic methods are methods that have special behavior. Just like normal methods, they can be called and return a value, but magic methods are used to give an object special behavior.

  • The constructor __init__, for example, is special in that it is invoked when an object is created.
  • The __set__ and __setattr__ methods are called when a value or property is set on the object.
  • The __repr__ and __str__ methods determine how an object is represented as a string.

Read more here: http://docs.python.org/reference/datamodel.html#special-method-names

Sjoerd