Top.Mail.Ru
Python
? ?
LiveJournal for python.
View:Personal Journal.
View:Friends.
View:Calendar.
Missed some entries? Then simply jump to the previous day or the next day.

Tuesday, April 25th, 2006

Subject:Option Explicit
Time:3:20 pm.
Mood:Image frustrated.
VBS has a lovely little command you can include at the start of every file, Option Explicit, this requires variables to be declared prior to use.

Now, Python instantiates variables when they are first assigned to, this allows the following hell:


class foo
  def __init__(self, thing="new")
    self.thing = thing

bar = foo()
bar.thin = "old"
print bar.thing


Prints new, I was wanting old.

Hmm, weak example, but, the point is, a simple type-o in a member variable when setting it (provided that it has been intialised before it's used) is next to impossible to detect, possibly resulting in horrible obscure bugs. The above case will print new, when I was meaning for it to print old, but missed one keypress and didn't get an "ooh that member variable doesn't exist mate!" message.

Is there any python-like way of doing Option Explicit? Can member variables not have docstrings and be pre-defined? Seems the only way to acheive this is to set self.__privateThing and use class.getThing and class.setThing, which I'm not keen on for various (religious) reasons.
Comments: Read 21 orAdd Your Own.

LiveJournal for python.

View:User Info.
View:Friends.
View:Calendar.
View:Memories.
Missed some entries? Then simply jump to the previous day or the next day.

Image