1

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)?

2 Answers 2

1

conjugate is a method of int, which can be applied to int objects.

To demonstrate, all of these work:

x = 2; x.conjugate()
(2).conjugate()
2 .conjugate()  # note the whitespace

Since the python parser has special handlings of numeric literals, it doesn't allow this awkward syntax:

 2.conjugate()

As explained by @ignacio, this is because the parser sees 2. and treats it as a float, i.e. like (2.)conjugate(), which is not a valid syntax.

However, other than using it with this syntax, the operation is allowed, as demonstrated above.

As for __add__, the __add__ method allows types to define/override how the + opertor applies to them. When you do a+b, the interpreter invokes either a.__add__(b) or b.__add__(a) (the mechanism is more complex than that).

Ordinarily, you shouldn't have to call __add__ directly.

Sign up to request clarification or add additional context in comments.

Comments

0

The parser sees the "." and thinks that a float is expected.

>>> (2).conjugate
<built-in method conjugate of int object at 0x2242140>

For the rest:

Python Language Reference, Data Model section

1 Comment

Technically speaking it's the lexer rather than the parser that makes the lower-level decisions about what constitutes a source "token". Since 2. is a valid token, the parser never gets to see the period.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.