Newbie: anything resembling static?
Paul Rubin
http
Tue Feb 11 18:25:28 EST 2003
phil at dspfactory.com (Phil Rittenhouse) writes:
> 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.
Sharing the class instance around doesn't seem any harder (or any
different) than sharing the function object around. If you run
something like
class _port:
def __init__(self):
self.initialized = false
def do_init(self):
self.initialized = true
...
def send_byte(self, x):
if not self.initialized:
self.do_init()
xmit(x)
send_byte = _port().send_byte
then you can use send_byte just as in your earlier definition.
More information about the Python-list
mailing list