PDA

View Full Version : Python absolute newbie question


realtimethrill
03-10-2009, 08:28 PM
Hi
I wanted to verify that number literals are objects in Python as they are in Ruby, so I typed in dir(1) at the console.

As I understand it, the + operator is actually calling __add__ behind the scenes.

If that's right, is there any particular reason why 1.__add__(2) doesn't work?

Apologies if this is a little basic!

oesxyl
03-10-2009, 09:20 PM
Hi
I wanted to verify that number literals are objects in Python as they are in Ruby, so I typed in dir(1) at the console.

As I understand it, the + operator is actually calling __add__ behind the scenes.

If that's right, is there any particular reason why 1.__add__(2) doesn't work?

Apologies if this is a little basic!
python is pure oop. the reason why 1.__add__(2) don't work is that the interpreter don't know what type is 1. try this:

one = 1
one.__add__(2)


best regards

realtimethrill
05-21-2009, 10:02 PM
Thank you for the reply.

What confuses me is that 1.2.__add__(2) does work. :confused: