Re: [Tutor] Syntactical question / OT Lisp

2005-01-21 Thread Alan Gauld
> 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...)

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Syntactical question / OT Lisp

2005-01-20 Thread Kent Johnson
Liam Clarke wrote:
Hi all, 

I've got a module called foo.py
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?
Is that right?
Yes, after
  import foo
but it's really ugly and a very bad idea. You should do something different like
  parrot.Sketch(self.config)
or even put the config into a module of its own if you really want a globally 
available configuration.
OK, so why is this so bad? Because foo and parrot depend on each other. Neither one can be used 
independently. You can't test parrot.Sketch() without creating a foo.app. You can't reuse 
parrot.Sketch() in another module named bar. You can't even restructure foo.py to make app a local 
variable of a main() function, for example.

Don't do this. Really. This way lies spaghetti code and intractable bugs and throwing the whole mess 
away and starting over doing it right this time.

Try to organize your modules so the dependencies form an acyclic directed graph. In other words, 
don't have any dependency cycles like foo depends on parrot depends on foo.

Kent
Regards,
Liam Clarke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Syntactical question / OT Lisp

2005-01-20 Thread Liam Clarke
Hi all, 

(side note - the net is not a luxury when attempting to learn to code) 

Just pondering my coding efforts, and just wanted to clarify something. 

I've got a module called foo.py

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?

Is that right?

Regards,

Liam Clarke
-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor