Tuesday, June 19

Learning Python: pyDoc, scope, arguments

Today's first topic is doc in python. Python provides a series of methods for us to understand objects, functions, etc. Here I write down some common shortcuts.
import sys
dir(sys)
This will display all possible attributes (including functions and data items) for one particular module. Some come with double underscore both at the beginning and the end, meaning that they are operation overloading functions.

Second is help. Use as the same as the dir but this will display all possible documentation/long comments for a module.

Function in python is really two steps: create a function object, assign it to a name. Just like normal assignments. Python also support creating function at runtime like this:
if a == True:
  x = 1
  def func2():
    # do someting
Polymorphism in Python lies in the functions: what will the function do totally depends on what type of input it takes in every time. From my algorithm course, my professor tells us most of the time an algorithm goes wrong because type mismatch: you give it a wrong type of input. Now the book tells us not to set any type limitations on your function, let it shine and raise error if something goes wrong. Theoretically to prevent type mismatch you have to be clear what king of inputs this function is able to deal with, which goes opposite of the freedom of Python. Maybe Python is trying to find the balance: instead of strictly define the type of input (like C) or take in whatever you give me, Python coders might keep a clear mind about several types of input they could afford to. But,

"If it does, it will be limited to working on just the types you anticipated when you wrote it, and it will not support other compatible object types that may be coded in the future. "

What? Anyway, this needs to be figured out sometime.

Scope is a boring topic. Everyone is trying figure out which belongs to whom. Python defines its own scope rule.


Only E makes me somehow confusing: as book says, this is for the nested def or lambda functions to find their local variables, because in some cases their locals might be outside of themselves; it will go up until it finds the first matching one.

"a function object that remembers values in enclosing scopes, even though those scopes may not be around any more. "

This behaves similar to blocks in Ruby, but little bit different. The book states that this should not be a normal case in everyday programming for Python.

Moreover, outer variables might be overrode by inner ones with the same name. Consider following case:
def hider():
open = 'spam' # Local variable, hides built-in
...
open('data.txt') # This won't open; turns into a local variable
To clearly use global variable in a local env, say in a function, one could use global statement. It is used to define that, ok, x here should be the global one, not anyone else.

Next topic, arguments in functions. Mainly about passing arguments. Python supports 4 ways of passing arguments, enabling us to pass exact number, arbitrary number of parameters with or without default values:
def func(a=1,b,c):
  #do something

>>>func(1,2,3)
>>>func(a=1,c=3,b=2)
>>>func(2,3)

def func(a, *args):
  #do more

>>>func(1,[1,2,3],"A")
>>>func(1)
Replacing * with ** then extra arguments will be taken into and group as a dictionary, otherwise list. Note if you use **, the input must be explicitly indicated with their keys.

Python has a very clear series of rules on how to use and understand each of these ways:
  1. Assign nonkeyword arguments by position.
  2. Assign keyword arguments by matching names/keys.
  3. Assign extra nonkeyword arguments to *name tuple.
  4. Assign extra keyword arguments to **name dictionary.
  5. Assign default values to unassigned arguments in header. 
At last, about how those arguments got passed - for immutable ones, only their values got actually passed into the function; for mutable ones like list, the book calls they are passed "by pointer", which means they could be changed in the function. There is no magic there, works just normal variables.






1 comment:

  1. Really nice blog post.provided a helpful information.I hope that you will post more updates like this Ruby on Rails Online Training India

    ReplyDelete