Thursday, April 02, 2009

Python Basics

Python Basics
Functions
  • Definition: Functions in Python are defined with the following syntax:
    def funct(arg_11, arg_2, ..., arg_n):
    print "This is a function."
    return value
    Any Python code, including other function and class definitions, may appear inside a function. Functions may also be defined within a conditional, but in that case the function's definition must be processed prior to its being called. Python does not support function overloading but does support variable number of arguments, default arguments, and keyword arguments. Return types are not specified by functions.
  • Arguments: Function arguments are passed by value so that if you change the value of the argument within the function, it does not get changed outside of the function. If you want the function to be able to modify non-local variables, you must declare them as global in the first line of the function. Note that if you declare any variables as global, that name cannot be reused in the argument list, i.e. this would cause an error:
    function double(x):
    global x
    x = x*2
    return
    double(x)
    Instead this could be done
    function double(n):
    n = n * 2
    return n
    x = double(x)

    Or
    function doubleX():
    global x
    x = x * 2
    return
    doubleX()

  • Default Arguments: A function may define default values for arguments. The default must be a constant expression or array and any defaults should be on the right side of any non-default arguments.
    def square(x = 5):
    return x*x
    If this function is called with square(), it will return 25. Otherwise, if it is called with square(n) , it will return n^2.
  • Variable length argument lists: Variable length arguments are supported by being wrapped up in a tuple. Before the variable number of arguments, zero or more normal arguments may occur:
    def var_args(arg1, arg2, *args):
  • Keyword arguments: Functions can also be called using arguments of the form keyword = value:
    def player(name, number, team="Florida"):
    print(name + "wears number " + str(number) + "for " + team)
    player("Matt Walsh", 44)
    player(number = 44, name = "David Lee")
    player("Anthony Roberson", number = 1)
    player(name = "J.J. Redick", number = 4, team = "Duke")
  • Return: Values are returned from the function with the return command: return var. You can not return multiple values, but that can be achieved by returning an array or object. Return immediately ends execution of the function and passes control back to the line from which it was called.
  • Variable Functions: Python supports the concept of variable functions. That means that if a variable can point to a function instead of a value. Objects within a method can be called similarly.
    def test():
    print 'This is a test.'
    var = test
    var()
    #this calles test()
    var = circle.setRadius
    var(3)
    #this calls circle.setRadius(3)

No comments:

Post a Comment