Newbie: anything resembling static?

Jp Calderone 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:
>             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.
> 

  class SerialBlob:
    def __init__(self):
        self.initialize()
    def initialize(self):
        # do stuff
    def send_byte(self, x):
        xmit(x)


> Thanks!
> Phil
> -- 
> http://mail.python.org/mailman/listinfo/python-list

-- 
|     This 
|   signature
| intentionally
|    8 lines
|     long.
|  (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
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20030211/157b5fac/attachment.pgp>


More information about the Python-list mailing list