self naming class?

Serge Rey serge at rohan.sdsu.edu
Fri Oct 19 00:09:29 CEST 2001


On Thu, Oct 18, 2001 at 10:22:27AM -0700, Chris Barker wrote:
> Luigi Ballabio wrote:
> > At 10:05 PM 10/18/01 +1000, Serge Rey wrote:
> > >i'm trying to create a class that assigns a name attribute in such a way
> > >that the name is set equal to the name of the instance. for example:
> > >
> > >test = MyClass()
> > >
> > >so that test.name == "test"
> > I see a phylosophical problem with the above, which is, "test" is not the
> > name of your instance but rather the name of a reference to such instance.
> 
> Luigi is right, and while I'm sure that someone on this group could find
> a way to do it, you probably don't want to. What you need to do is step
> back and ask yourself why you want to so this at all. In general, having
> a link between the data and actual variable names can make it a little
> easier to think about the code, but takes away a lot of flexibility, and
> is ripe for the kind of problems that Luigi has pointed out,
> particularly in Python.
> 
thanks to you and luigi for pointing out the problems with what i was
trying to do.

learning of the problem with this is more important to me than getting a
"fix".

> When I want a bunch of instances to have useful names that I can refer
> to them by, I usually put them in a dictionary:
> 
> d = {}
> dict["test"] = MyClass()
> dict["test2"] = MyClass()

this was one of my early solutions, but then you have to access the
instance via the dictionary key, rather than directly.

> 
> or if you want the instance to have a name atribute:
> 
> thisname = "test"
> dict[thisname] = MyClass(name = thisname)
> 
> Note, however, that I probably wouldn't do this (or what you have
> proposed) at all. If the object has a name attribute, than you don't
> want a duplication of data, there is too much of a chance of it getting
> mixed up. You might do:
> 
> thisname = "test"
> instance = MyClass(name = thisname)
> 
> dict[instance.name] = instance
> 
> that way you are guaranteed that the names match.

yes, i'll have to rework my thinking along these lines.

> 
> A good general rule is that if it is hard to do something in Python, you
> are probably trying to solve a problem in a non "Pythonic" way, and you
> should step back and find a more natural way to do it.

a very good rule. it is going up over my desk :)


-- 
Sergio J. Rey	http://typhoon.sdsu.edu/rey.html
GPG fingerprint =  16DB 4934 E0F1 B386 AE81  D379 914C 33E5 F690 95DF
"The primary purpose of the DATA statement is to give names to constants;
instead of referring to pi as 3.141592653589793 at every appearance, the
variable PI can be given that value with a DATA statement and used instead
of the longer form of the constant. This also simplifies modifying the
program, should the value of pi change."
        - from a Fortran manual for Xerox computers
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 240 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20011019/da06ad5c/attachment.pgp>


More information about the Python-list mailing list