On 10/10/2010 3:07 PM, John Nagle wrote:

I understand how the current semantics fall out of the obvious
implementation. But I don't see those semantics as particularly
desirable. The obvious semantics for globals are similar, but
that case is so error-prone that it was made an error.

Nicely stated.

(If you want default values for an instance, you define them
in __init__, not as class-level attributes.)

Since it's unlikely that the language will change, perhaps a naming convention would help. I'm not sure I like this myself, but ...

Class attributes are often used as "class constants", so how about naming them with UPPERCASE names, like other constants? When you choose to override one of these constants, like this:

   self.EGGS = 4

... the awkward looks of the statement serve as a hint that something special is happening.

#------------
class SpamMeal:
    EGGS = 2

    def __init__(self, egg_count=None):
        if egg_count:
            self.EGGS = egg_count

    def Report(self):
        print "This meal includes %d eggs." % self.EGGS

meal = SpamMeal()
meal.Report()    # "This meal includes 2 eggs."

meal = SpamMeal(3)
meal.Report()    # "This meal includes 3 eggs."

meal = SpamMeal()
meal.EGGS = 4
meal.Report()    # "This meal includes 4 eggs."
#------------

-John Posner
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to