How do I get the possible "self attributes" of a Python class? -
for example how can define properties of class object in python? how can define happens when add 2 objects? or multiply them? or divide them? or print them? or if call 1 number of arguments?
i see things __mul__
, __add__
these called , rest?
this called operator overloading.
class human(object): def __init__(self, name): self.name = name def __add__(self, other): return '{0} {1}'.format(self.name, other.name) def __mul__(self, other): return self.name * len(other.name) def __str__(self): return self.name bob = human('bob') sam = human('sam') print sam + bob # calls __add__ print sam * bob # calls __mul__ print bob # calls __str__