Guido's comment that in python: "an expression does not compute a
value but yields an object" is, like "assignment is not the modification
of a storage cell, but the binding of a name to an object", a seminal
point. These two sentences should be somewhere prominent in the user
guide - in *BOLD* or underlined!
It has some implications I haven't fully thought out yet.
( I *was* prepared to argue that, to exploit interactive terseness,
all python expressions should yield values ( like in C or ICON )
and that side effect expressions ( like C: if ( ( n = read() ) == max ) ... )
should be allowed, as well as op-assignment ( +=, etc. ).
Mainly because python loops often seem to require some "forethought" :
perhaps not a bad thing for a programmer to be forced into :-), but in
interactive use, it can be an annoyance.
I am rethinking this position. )
It *DID* inspire me to discover that un-named objects also have a
name space.
(*example*):
>>> class Thing():
... pass
...
>>> T = ( Thing(), Thing(), Thing() )
>>> T
(<instance object at 7af78>, <instance object at 83470>, <instance object at 83)
>>>
>>> type( T )
<type 'tuple'>
>>>
>>> T[0]
<instance object at 7af78>
>>>
>>> type( T[0] )
<type 'instance'>
>>>
>>> for x in T :
... x.L = []
... x.i = 0
...
>>> T[0]
<instance object at 7af78>
>>> T[0].L
[]
>>>
>>> T[0].i
0
>>>
The application of this is that I would like to transform my tuple of
tuples into a tuple of objects without cluttering up the namespace
with what should be anonymous objects.
E.g. I have a list of tuples:
[ ( IdNumber, 'Na4-P2-O7', ( 'Na', 4, peak-count-for-Na ),
( 'P', 2, peak-count-for-P ) ) ... ]
( Perhaps this structure can be cleaned up a bit in it's logical grouping,
but the point is: )
References like: x[0][2][2] get to be a bit opaque. But each item is
a pair in a larger list, and I don't want/need them each to be named.
This example, however, leads me back to one of my other problems/complaints
with python classes:
I does not seem possible ( to me: I may not understand this correctly )
to initialize an object at it's creation. i.e. what I want to be able
to do is something like:
class Thing( y ):
Thing.x = y
Thing.i = 0
or
class Thing():
def init( me ):
me.L = []
me.i = 0
return me
Thing().init()
But I have been unable to find an acceptable syntax variation of the
above that is accepted.
BUT:
>>> Thing().L = []
Does work.
Though:
>>> a = ( Thing().L = [] )
Unhandled exception: undefined name: L
Stack backtrace (innermost last):
File "<stdin>", line 1
>>>
Obviously does not, since the parends make it look like comparison
is intended. ( Will this work when "equals" becomes '==' ? )
Actually, while writing this note, I have been swapping between
emacs & python to try examples, and I finally figured out how to
do what I want ( But I will go ahead and send this, instead of
just saying "Nevermind", for discussion sake. ):
>>> class Thing():
... def init(me):
... me.L = []
... me.i = 0
... return me
...
>>> T = ( Thing().init(), Thing().init() )
>>> T
(<instance object at 7add0>, <instance object at 836f0>)
>>> T[0]
<instance object at 7add0>
>>> T[0].i
0
>>> T[0].L
[]
>>>
>>> a = Thing().init()
>>> a
<instance object at 84908>
>>> a.i
0
>>> a.L
[]
>>>
I *still* think it would be nice if there was a way to MAKE classes
become initialized when created as part of their definition,
rather that by explicitly calling an init() function. ( Is there
a way that I'm missing ? ) But I can probably live with the above
method.
- Steve Majewski