A global or module-level variable?

Paul Rubin http
Tue Jan 22 15:00:47 EST 2008


Bret <bret.wortman at gmail.com> writes:
> nextport=42000
> 
> def getNextPort():
>     nextport += 1
>     return nextport

If you have to do it that way, use:

    def getNextPort():
        global nextport
        nextport += 1
        return nextport

the global declaration stops the compiler from treating nextport as
local and then trapping the increment as to an uninitialized variable.



More information about the Python-list mailing list