Calling an argument in a python function as part of the function definition? -


how can pass argument in function input within function definition?

for example:

def correlate1(vara1, vara2=vara1*2):     print "test" 

returns:

traceback (most recent call last):   file "test.py", line 321, in <module>     def correlate1(vara1, vara2=vara*2): nameerror: name 'vara1' not defined 

obviously, this:

    def correlate1(vara1, vara2=0):         if vara2==0:             var2=vara1*2                 print "test" 

but there way in function definition?

just set once function called:

def correlate1(vara1, vara2=none):     vara2 = 2 * vara1 if vara2 none else vara2      print "test" 

or more simple:

def correlate2(var1, var2=none):     var2 = var2 or var1 * 2     print "test" 

var2's default value expression can not contain var1 because var1 not exist until function called.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -