Newbie: anything resembling static?
exarkun at intarweb.us
Wed Feb 12 01:27:12 CET 2003
On Tue, Feb 11, 2003 at 12:53:52PM -0800, Phil Rittenhouse wrote:
> > >def foo():
> > > static count = 0
> > > print count
> > This sort of local state is specifically what objects are for... I'd
> > rather advocate using them than adding a keyword for this...
> I'm sure that is true in many cases, but is it always true?
> I'm thinking about something like a function to send a byte out
> a serial port. The first time it's called it needs to initialize
> the UART, but after that it doesn't. Something like:
> def send_byte(x):
> static initialized = False
> if not initialized:
> initialized = True
> If you used this function the way you might use print() for debugging
> purposes, it might be called in hundreds of places in a large project.
> If you wrapped it in a class, you'd have to take care of creating the object
> before anyone calls it and sharing that object around somehow so everyone
> can access it. It seems like a lot of complexity for what is supposed
> to be a very simple task.
> I'll admit I'm no OO guru, so if there's a better way to do it, let me know.
# do stuff
def send_byte(self, x):
| 8 lines
| (So sue me)
up 3 days, 4:28, 7 users, load average: 0.00, 0.02, 0.12
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Size: 196 bytes
Desc: not available
More information about the Python-list