Exception handling wart in Python

Don O'Donnell donod at home.com
Wed Nov 7 03:03:25 EST 2001


Leo Lipelis wrote:
[snip]
> ...  Point is, Python can be made better by getting rid of
> the "self.__" in front of every object attribute.  For instance, why not
> assume that ALL variables are instance variables, and have the other ones
> use some special notation?  The absolute majority of attributes you will
> use are instance attributes.

That's just not true.  The majority of variables in most of the method
defs I've seen are local variables: method arguments, loop counters,
indexes, temporary variables, etc.  And rightfully (by your own
reasoning) local variables are the ones which should not need any
qualification, as indeed they don't.  And, in fact, since local
variables are faster to access in execution time than instance, class or
module variables, which require a dictionary lookup, I often make a
local out of a frequently used instance variable to speed up execution
(or just use arg rather than self.arg for module arguments):

class Person:
    def __init__(self, name, gender, dateOfBirth):
        self.name = name
        if len(name) == 0:          # use name not self.name
            raise NameError
        self.gender = gender.lower()
        gender = self.gender        # make instance var local
        if gender != 'm' and gender != 'f':   # for quicker access
            raise GenderError
        ...

>      Why penalize the common case?  That's bad
> engineering.  You should always penalize the fringe case.  Doesn't that
> make sense to you?

It certainly does, and the common case is the local variable.

>      If you have 2 class attributes and 20 instance
> attributes, why would you penalize the 20 instead of 2?  That's insane.

I don't understand your argument here, both class and instance
attributes need to be qualified.

Cheers,
Don



More information about the Python-list mailing list