Explicit variable declaration

Jason Stokes glasper9 at yahoo.org.au
Fri Apr 25 01:23:36 EDT 2008


"Filip Gruszczynski" <gruszczy at gmail.com> wrote in message 
news:mailman.89.1209000606.12834.python-list at python.org...
>>  If you want to just declare that name exist, but doesn't want to
>>  declare the type, why don't you just do this:
>>
>>  def somefunc():
>>     nonlocal = nonlocal
>>     local = 0 # or None or [] or an initial value
>>     #
>>     return nonlocal * local
>
> Err.. I don't quite get. How it may help me? Could you explain?

Hi Filip,

In Python the standard patten for "declaring" variables is just to assign to 
them as they are needed.  If you want the effect of a declaration as you 
would do in C, you can just define the variable and initialize it to 0 or 
None.  (Or {} for a new dictionary, or [] for a new list.)

eg,

def collaterecs():
    recordscount = 0
    recordlist = []
    ...
   return recordlist






More information about the Python-list mailing list