Whither SmallScript? (was Re: Integer micro-benchmarks)
aleaxit at yahoo.com
Wed May 2 15:25:42 EDT 2001
"Darren New" <dnew at san.rr.com> wrote in message
news:3AF032C6.2F81D489 at san.rr.com...
> James A. Robertson wrote:
> > > were wondering. And add or remove instance variables
> > > dynamically, too (I don't think that one is so easy in
> > > Smalltalk).
> > Sure it is.
> > MyClass addInstVarName: 'foo'
> > MyClass removeInstVarName: 'foo'
> This is a little different from Python, tho.
> In Python, you add instance variables to individual instances. Not all
> instances of the same class must have the same set of instance variables.
> (In one sense, all instances of a class have the same instance variables,
> but one of those instance variables is a dictionary mapping instance
> variable names to values, and the syntax makes this mostly invisible.)
OK, you can frame it this way...
> I think if you want to add an instance variable to all instances of a
> you need to write a loop to iterate over all those instances. I'm not sure
> whether you could find them.
...however, when looking for any attribute (no syntax distinction
between 'variable' or 'method') over an instance object, if not
found in the __dict__ of the *instance*, it's next looked up in that
of the __class__ (and then up the __bases__ DAG thereof).
Thus, if somewhere you do
foo.__class__.newname = 23
now foo.newname, AND bar.newname for any instance bar sharing
as its class (or among its bases, if not otherwise shadowed by
inheritance) will also be 23. Specific instances may override that --
bar.newname = 45 now sets only the 'newname' attribute in the
dictionary of instance bar, without modifying that in other instances
such as foo -- but until and unless .newname is bound to something
specific for a given instance, it will delegate to the class and up the
Not a very common object model, yep, but I find it very useful in
many practical cases.
More information about the Python-list