NEWBIE: What's the instance name?

engsolnom at engsolnom at
Mon Dec 29 20:03:46 CET 2003

On Mon, 29 Dec 2003 07:10:15 GMT, "Paul McGuire" <ptmcg at>

><engsolnom at> wrote in message

>news:6q2vuv45lsah3epo9loa7l2tp9517makk4 at
>> Hi,
>> I've been using constructs(?) like the below.
>> def display(instance, start = None, end = None):
>>     if instance.__class__.__name__ == 'UPCA':
>>         do some UPCA stuff
>>     if instance.__class__.__name__ == 'UPCE':
>>         do some UPCE stuff
>> launched by:
>> lx = UPCA(sx), where sx is a string
>> then:
>> display(lx)
>Why don't you just define a display() method on the UPCA and UPCE classes,
>and instead of display(lx), call lx.display().  If UPCA and UPCE inherit
>from a common base class (presumably with an abstract or default display()
>method, but not necessarily), this is called "polymorphism".
>Something like this:
>class StringDisplayer(object):
>    def __init__(instance, instring ):
>        instance.dispString = instring
>class UPCA(StringDisplayer):
>    def display(instance, start = None, end = None):
>        # UPCA-specific display stuff goes here
>class UPCE(StringDisplayer):
>    def display(instance, start = None, end = None):
>        # UPCE-specific display stuff goes here
>sx = "abc"
>lx = UPCA(sx)
>Your example also uses "instance" for the pointer to self, which works okay,
>but is non-traditional; the conventional name for the self pointer is
>In general, any time you are if-testing on the class of an object, you are
>probably doing something polymorphic.  If there is common behavior also, put
>this in the base StringDisplayer class, and invoke it using
>"super(UPCA,instance).display()" in the display() method of the UPCA class,
>and likewise in the UPCE class.

Thanks to all for the good responses to my original post.

A couple of comments....

After thinking about it, (right after hitting enter, of course), I
concluded I can not 'discover' the name of the instance, for the same
reason that if x = 10, I can't ask '10' what the name of its reference
is. It simply doesn't know, and, as one responder pointed out, doesn't
care . All your replies confirmed this.

As to the (good) suggestions I use :

lx = UPCA(<string>)

I considered this approach. The reason I rejected it is that the
actual program has a total of 13 UPCA-like classes (different names,
of course). In use, each class may have one or many instances at a
time, as determined by the user script. My idea was to include the
calling instance 'name' in the display ...not strictly needed, but a
nice touch.

I could use:
 display(lx, 'lx')
I'm thinking about'd be a user option to include the 'lx'.

Why the display in the first place? The user *may* desire to display
the class output, probably most often while trouble-shooting a user
script problem.

Some of the classes have a common (or near common) display routine,
others are totally different. If I use lx.display(), the display code
has to be contained as a method in each class. I chose, for ease of
maintenance, to make a display module with all the display routines
gathered together in one spot. . In the event a future programmer
decided to make a change to the display routine for a given class, or
needed to add a class/display, I feel there's less chance of his/her
messing up the UPCA-like classes. I guess I'm a believer in
classes/methods doing a limited number of things, and doing them well.
Is that a C hang-over?

A thought:
Another idea might be to make a display base class, and inherit the
proper display method in each UPCA-like class. But that still leaves
the problem of passing the instance name to the display method.

As to the 'ishexdigit' question, thanks for suggested
approaches...lots of meat there for this newbie to bread-board and try


More information about the Python-list mailing list