Why not 3.__class__ ?

David Bolen db3l at fitlinxx.com
Tue Oct 9 18:28:03 CEST 2001

"Markus Jais" <mjais at web.de> writes:

> I haven't followed the discussion
> but to write 3.foo is something I would like to have in Python
> I am a big fan of Ruby and there you can write
> for example
> -4.abs

But is there any practical reason you'd ever write that (e.g. instead
of just writing "4" and being clearer?)

I could understand if you had something like:

   value = -4

but why would you ever invoke a method on a numeric literal in your source?

I can appreciate how such orthogonality may have an attraction, but am
not sure I see any practical benefits to trying to fit such a thing
into the Python parser.

> which gives 4 (of course)

Is it?  "Of course" that is.  I would think you could read that as:

	-(4.abs)	which would be -4
	(-4).abs	which would be 4

In Python, -4 is actually the unary - operator applied to the numeric
literal 4, whereas I believe in Ruby it's a numeric literal -4, so
there's a basic difference in the parsers.  But it does mean that
there's some ambiguity as to how you might read the expression,
particularly if you want to be consistent with an object invocation.
For example, using an object instead of a literal:


If this was treated, just as your numeric literal case, it would be
retrieving the value of "obj" (somehow), negating it, and then calling
the abs method on that resulting negated value?

Of course, in both Python and Ruby it instead calls the obj.abs method
and then negates the result.  In Python, it's the same unary -
operator and precendence in effect in both cases, while in Ruby in one
case it's a numeric literal and the other the unary operator.

-- David
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \

More information about the Python-list mailing list