Objects in Python

MRAB python at mrabarnett.plus.com
Wed Aug 22 16:31:51 EDT 2012


On 22/08/2012 20:45, lipska the kat wrote:
> On 22/08/12 20:03, Evan Driscoll wrote:
>> On 08/22/2012 12:46 PM, lipska the kat wrote:
>>> If you can show me a 'type' that cannot be assigned to
>>>
>>> a
>>>
>>> in the same scope then I would be most interested to know, I haven't
>>> found one yet.
>>
> [snip]
>
>>
>> Second, this concept isn't *so* unfamiliar to you. If I give you the
>> following Java code:
>>
>>    void foo(Object o) { ... }
>>
>> and ask what type 'o' is, there are kind of two answers. The first is
>> that 'o' is an 'Object'. But you can't make an Object -- that's an
>> abstract class. (IIRC. If it's not then just bear with me; you get the
>> idea. :-)) So from a strictly static type-theory point of view, 'foo' is
>> unusable because it takes a type which you can never create. But of
>> course that's not the case, because in actual Java 'o' has some dynamic
>> type which is a subclass of 'Object'.
>
> Well I think this is where I'm struggling a bit.
>
> looking at this method declaration I can see that the method takes an
> argument of type Object (and just FYI class Object is not abstract and
> you can do Object o = new Object()) and does not return a value.
> I know that for the lifetime of this JVM, whatever o turns out to be it
> will always be an Object. I can't assign a primitive to o as ints chars
> floats etc are certainly not Objects. There are certain invariants that
> give me a warm and comfortable feeling inside.
>
> compare this to a function declaration in Python
>
> def foo(self):
>
[snip]
That's not actually a declaration but a definition. :-)

The function's body is bound to the name at runtime, so:

     def double_it(x):
         return x * 2

is not far from:

     double_it = lambda x: x * 2

The only declarations are "global" and "nonlocal" (and the latter
exists only in recent versions of Python).



More information about the Python-list mailing list