[Tutor] self knowledge

Alan Gauld alan.gauld at freenet.co.uk
Sat Oct 22 01:25:32 CEST 2005


Hi Ethan,

> When using the class in practice, I've found it natural to
> create instances of Foo so that the instance itself is called
> name.  Thus, I find myself doing things like:
>
>>>> a=Foo('a')
>>>> b=Foo('b')
>

This is very common at the interactive prompt.
However its nearly always the wrong thing to do in a real world
type program. For one thing how will the code elsewhere in your
programme know about these new object names? You would
need to find a way for every point in your code to gain awareness
of all the names currently in use! In fact Python does provide
ways of doing that because the mnames are hekld in system
dictionaries which you can access but its extre,mekly kludgy!

Much better is to create your instances in a dictionary with the
key being the name. Thus in your example:

>>> objects = {} # empty dictionary
>>> objects['a']=Foo('a')
>>> objects['b']=Foo('b')

You then access the objects with

objects[name].method()

slightly more typing but much easier to use because you can
iterate over all the objects in the dictionary with

for obj in objects:
   obj.dosomething()

Otherwise you would need to know all the object names
and call each in turn or resort to "black magic" to access
the Python internal dictionaries.

> I want the name attribute to depend only on the name of the
> original variable

Now that's a different proposition.
Do you want the name to depend on the prebviously created
variable or the variable name to depend on the object attribute?

If you want the object attribute to depend on the variable name
thats very strange. What happens when you pass the object to
amother namespace? Or if you save to a file or database and
then restore, will it always be associated with the same variable?

Can you explain why you believe that would be useful?

>>>> c=a
>>>> c.name
> 'a'
>
> is the desired behavior.

OK, Thats different again.

That could be done as shoewn above with

>>> c = objects['a']
>>> c.name

> First, my immediate inclination is to try and think of
> a way for the object to know its own name (at the time
> its instantiated) without being told

variables in python are merely references to an object, the object is not 
actually called by the name of the variable. Consider:

a = C() # an instance of c
b = a    # both a and b point at the instance

which is the name of the C instance? Is it a or b or neither?

>>> id(a)
>>> id(b)

shows that the objects real identity is a number which is
unrelated to either 'a' or 'b'

> Second, because this seems awkward, I strongly suspect
> that there's a pretty fundamental problem with the way
> I'm approaching the problem.  When the name of a variable
> is itself information which might be useful, what's the
> right way convey this information?

Yes you are trying tonsolve the wrong problem.
Instances created dynamically at runtime are best kept in
a container (list or dictionary) and managed as a group or
selected on demand. A dictionary is usually the best option.

After all thats why Python uses dictionaries under the hood
for its own purposes!

FWIW I give an example of this problem in the OOP topic
of my tutor, in the bank account example where we need to
store a collection of bank accounts by account ID.

HTH,

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list