Why self?

Robb Shecter rs at onsitetech.com
Tue Jul 9 13:15:09 EDT 2002


Alex Martelli wrote:
I think I realized why several people haven't gotten what I mean here:


>>
>>String name = (String) aVector.element(0)
>>
>>Python doesn't have any problems with implicitness here...


A cast in Java is _not_ like a cast in C, although it looks like it.  As 
far as I remember, a cast in C really lets the casted variable be 
treated like the new type. (?)

In Java, no action is really taken during a cast.  No String 
representation is generated above.  It's actually a way to get around 
the static typing.  Normally, aVector always returns Objects, and you 
only have access to the methods in the Object class.  The cast tells the 
compiler that you're convinced it's a String, and thus the compiler will 
let you send a message listed in the String class's API.

So again, I think that this does highlight the implicit nature of Python 
- when you grab an object out of a container, you can send whatever 
message you want to it.  You don't have to explicitly state what class 
you think it is.

Robb




More information about the Python-list mailing list