...

Python absolute newbie question

realtimethrill
03-10-2009, 07: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, 08: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, 09:02 PM
Thank you for the reply.

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

mr.jsnerd
01-18-2010, 07:23 PM
in 1.2, it thinks that 2 is a property of one (i think, im a python newb too)

mr.jsnerd
01-18-2010, 07:24 PM
what does it return? 4? 2? 3.2?

Samhain13
03-24-2010, 01:38 PM
This works.
int(1).__add__(2)

So maybe the interpreter needs to know that 1 is an integer. Same result with:
n = 1
n.__add__(2)

If 1.2.__add__(2) works, maybe that's because the presence of the "." automatically makes 1.2 a float, while simply "1" may be confused with a boolean? Dunno, but it seems to make sense. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum