Newbie: anything resembling static?
Phil Rittenhouse
phil at dspfactory.com
Tue Feb 11 15:53:52 EST 2003
> >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:
do_init()
initialized = True
xmit(x)
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.
Thanks!
Phil
More information about the Python-list
mailing list