I read somewhere that everything in python is an object. I thought, how about the number 2? If I type "dir(2)" into the python interpreter, I get the following output:
dir(2) ['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']
Evidently the number 2 is an object with these attributes. I get several names of attributes the number 2, like __add__ and conjugate. But if I try 2.conjugate(), I get an error
2.conjugate() SyntaxError: invalid syntax
However dir(2j) has conjugate as a method and I do not get an error when I use the method conjugate.
2j.conjugate() -2j
and
2.0.conjugate() 2.0
also gives me no problems.
More weird things, 2.__add__() and 2.__add__ gives an error even though it is an attribute of 2. I think add refers to the + operation. So why does it show up as __add__ in the list of attributes and not as +? Is dir(2) the list of variables and methods of 2 or does it list other things? What is the difference between __thing__ and thing? When can you call a method the regular way, like object.method() and when do you have to do funky things like 2+2 instead of 2.add(2)?