[Tutor] Syntactical question / OT Lisp
Alan Gauld
alan.gauld at freenet.co.uk
Fri Jan 21 23:25:03 CET 2005
> foo.py -
>
> import parrot
>
> class Bar(model.Background):
>
> def __initialize__(self, event):
> #Just a pythoncard variant on init
> self.config=self.loadCfg()
>
>
> def loadCfg():
> #get some cfg stuff, return as dict
> return cfgDict
>
> def on_aBtn_mouseClick(self, event):
> parrot.Sketch()
>
> app=Bar(main.application)
> app.mainloop()
>
>
> If I wanted the function parrot.Sketch() to access that config
> dictionary, I would reference it as
>
> foo.app.config?
You could but it would be very bad practice and much better
to pass the object reference into Sketch.
def on_aBtn_mouseClick(self, event):
parrot.Sketch(self)
def Sketch(anObject):
....code ...
configINfo = anObject.config
Even better make the app object return the actual config
bits that parrot needs via a method:
def Sketch(anObject):
... code...
myConfigItem1,mycOnfig2 = anObject.getSketchConfig()
All of these options make both parrot and the foo object
more easily reusable and much less liable to break when
you make changes to the config data.
Alan G
(Back from a week's trainig camp...)
More information about the Tutor
mailing list