Re: [Tutor] Scope and elegance revisited

2008-01-09 Thread Michael H. Goldwasser
On Wednesday January 9, 2008, Kent Johnson wrote: >James Newton wrote: >> Hi Python Purists! >> >> I want all instances of a given class to share a piece of information, >> but I want to set that information on the fly. I have found that this >> works: >> >>

Re: [Tutor] Scope and elegance revisited

2008-01-09 Thread Kent Johnson
James Newton wrote: > The folder in question contains images for counters for a board game. > Each player instance will use a separate counter image, but all counter > images will be chosen from the same folder. > > It looks as if the Borg pattern would make all players use the same > counter imag

Re: [Tutor] Scope and elegance revisited

2008-01-09 Thread James Newton
Kent Johnson wrote: >> To give you the context: my application allows you to select a skin for >> the user interface. I want to set the access path to the skin folder as >> a class variable, so that all instances of that class use images from >> the appropriate folder. The access path will be re

Re: [Tutor] Scope and elegance revisited

2008-01-09 Thread Kent Johnson
James Newton wrote: > Hi Python Purists! > > I want all instances of a given class to share a piece of information, > but I want to set that information on the fly. I have found that this > works: > > > class Foo(object): > # class_variable = None # There is no real need to declare this

[Tutor] Scope and elegance revisited

2008-01-09 Thread James Newton
Hi Python Purists! I want all instances of a given class to share a piece of information, but I want to set that information on the fly. I have found that this works: class Foo(object): # class_variable = None # There is no real need to declare this def __init__(self):

Re: [Tutor] Scope and elegance

2008-01-08 Thread Tiger12506
> Thanks Tiger12506! > > This has helped me understand the function(*tuple) syntax, as well as > providing me with a concrete example. > > James Cool. ;-) Here's another, totally unrelated to counters and boards. Kinda the opposite use of the * syntax I used earlier. def printall(*li): for x

Re: [Tutor] Scope and elegance

2008-01-07 Thread Tiger12506
I like this. class Counter: def __init__(self): self.score = 0 def incr(x, y): self.score += 2*x+3*y class Board: def __init__(self): self.counter = Counter() self.curcoords = (0,0) def update(self) self.counter.incr(*self.curcoords) Whatever OOP term is used to d

Re: [Tutor] Scope and elegance

2008-01-07 Thread Ricardo Aráoz
James Newton wrote: > Hi Python Practicioners! > > I'm trying to come to terms with how Python handles the scope of > objects. In particular, I'd like to create an instance of one class and > make it available inside instances of other classes. Something like: > > > # Test 1---

Re: [Tutor] Scope and elegance

2008-01-07 Thread Kent Johnson
Torsten Marek wrote: > Maybe I'm spoiled from programming too much Java in the last year, but Hmm. Would that be spoil 3 a: to damage seriously : ruin or spoil 4 b: to pamper excessively : coddle ? ;-) > IMHO it's a good idea to put the singleton instance into the class > itself rather than int

Re: [Tutor] Scope and elegance

2008-01-07 Thread Kent Johnson
Marc Tompkins wrote: > I'd like to get the party line on this as well. For a while now, I've > made a habit of defining an empty class called Global (Mr. Newton's > could be called Board, of course) at the top of my apps, and using its > attributes as if they were global variables. It works, o

Re: [Tutor] Scope and elegance

2008-01-07 Thread James Newton
Kent Johnson [mailto:[EMAIL PROTECTED] wrote: > Why does a Counter need to know about screen position? > It sounds like the Counter might be doing too much. > Maybe the Counters should be attributes of the Board > which can ask them their index numbers and do the > appropriate drawing? > Maybe the

Re: [Tutor] Scope and elegance

2008-01-07 Thread Torsten Marek
Hi, I'll try to swallow down the "Globals are eevil" comment, there's enough literature on that already. Maybe I'm spoiled from programming too much Java in the last year, but IMHO it's a good idea to put the singleton instance into the class itself rather than into some module. This way, you (c

Re: [Tutor] Scope and elegance

2008-01-07 Thread Marc Tompkins
On Jan 7, 2008 12:07 PM, James Newton <[EMAIL PROTECTED]> wrote: > Hi Python Practicioners! > ... > I am not so much concerned in getting this to work (I can already do > that); I am more concerned with understanding how to treat this in the > most Pythonesque way. > I'd like to get the party lin

Re: [Tutor] Scope and elegance

2008-01-07 Thread Kent Johnson
James Newton wrote: > # So that I can call one of its functions from another instance > class Bar(object): > def doBar(self): > # I'd like be able to call a "global" object, but this fails: > # global name 'foo_instance' is not defined > return foo_instance.doFoo() It

[Tutor] Scope and elegance

2008-01-07 Thread James Newton
Hi Python Practicioners! I'm trying to come to terms with how Python handles the scope of objects. In particular, I'd like to create an instance of one class and make it available inside instances of other classes. Something like: # Test 1- # I want a "globa