NEWBIE: What's the instance name?

Alan Gauld alan.gauld at btinternet.com
Mon Dec 29 04:03:51 EST 2003


On Sun, 28 Dec 2003 18:15:49 -0800, engsolnom at ipns.com wrote:
> 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)

Any reason not to use an OO style and do:

lx = UPCA(sx)
lx.display()

That way you miss out all those horrible if instance... checks.
Its much easier to maintain too since if you add a new class you
only need write a display method in the class, you don;t need to
extend the if/elif chain too.

> What I'd like to do is display is the instance name. Is it hiding
> somewhere?

What is the instance's name? It is an object referenced by a
variable(or maybe by several variables...). Unless you have
defined a name attribute then the instance has no name.

> Also, if I have a string 4 chars long, representing two bytes of hex,
> how can I *validate* the string as a *legal* hex string?

hexdigit = "-0123456789abcdefABCDEF"
def ishexdigit(n): return 0 not in [x in hexdigit for x in n]

Not fast but it should work...

> Is there a 'ishexdigit'? I could parse the string, but that seems
> "un-Pythonish"

How else would ishexdigit work? :-)

Alan G.

Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Python-list mailing list