Image

Imageeyeh8u wrote in Imagepython_dev 😡frustrated

Option Explicit

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.