a fairly ugly/kludgy way to get 'aliases' in Python

Cliff Wells LogiplexSoftware at earthlink.net
Fri Jan 17 19:12:45 CET 2003


On Thu, 2003-01-16 at 15:52, Jonathan P. wrote:
class A:
>   def __init__(self):
>     self.long_descriptive_name=0
>     self.long_descriptive_name2=10
> 
>   def x(self):
>     D_=self.__dict__
>     alias1='long_descriptive_name'
>     alias2='long_descriptive_name2'
>     D_[alias]=D_[alias2]*D_[alias1]
>     # instead of self.long_descriptive_name =
>     #  self.long_descriptive_name*self.long_descriptive_name2
> 
> Question: 
>   Will the dictionary lookup 'D_[alias]' impose a
>   performance penalty versus 'self.long_descriptive_name' or
>   will the compiler do the same thing internally anyway in
>   both cases?

I don't think performance will be affected either way (it's all
dictionary lookups).  Still, I wonder why you don't just use:

class A:
    def __init__(self):
        self.long_descriptive_name = 0
        self.long_descriptive_name2 = 10

    def x(self):
        alias1 = self.long_descriptive_name
        alias2 = self.long_descriptive_name2
        self.alias = alias2 * alias1

which would seem to be the equivalent, more readable form.

-- 
Cliff Wells, Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 x308  (800) 735-0555 x308






More information about the Python-list mailing list