1/23/2014

(python study) about function (example source code)

__author__ = 'mare'


def Times(a, b):
 return a*b

print(Times)
#

print( Times(10, 10) )
#100

myTimes = Times;
r = myTimes(10, 10);
print( r )
#100

#return

def setValue(newValue):
 x = newValue

retval = setValue(10)
print( retval )
#None

def swap(x, y):
 return y, x
swap(1,2)
#(2,1)

a, b = swap(1,2)
#a = 2, b = 1

x = swap(1,2)
print( type(x) )
#


#more complex function
def intersect(prelist, postlist):
 retlist = []
 for x in prelist:
  if x in postlist and x not in retlist:
   retlist.append(x)
 return retlist

list1 = "SPAM"
list2 = "AM"

list3 = intersect(list1, list2)

print(list3)
#['A', 'M']


#parameters
a=10
b=20

def sum1(x,y):
 return x+y

print( sum1(a,b) )
#30

x = 10
def sum2(x,y):
 x = 1
 return x+y

print( sum2(x, b), x )
#21 10

def change(x):
 x[0] = 'H'

wordlist = ['J', 'A', 'M' ]
change(wordlist)
print(wordlist)
#['H', 'A', 'M']



def change(x):
 x = x[:]
 x[0] = 'H'
 return None

wordlist[0] = 'J'

change(wordlist)
print(wordlist)
#['J', 'A', 'M']


#scoping rule
x=1
def func2(a):
 x=2
 return a+x
print( func2(10) )
#12

def func3(a):
 global x
 x = 20
 return x+a

print( func3(10 ), x)
#30 20

dir( __builtins__ )
#you can see inner function names

#arguments
def Times(a=10, b=20):
 return a+b

print( Times() )
#30

print( Times(5) )
#25

#keyword arguments
def connectURI( server, port):
 str = "http://" + server +  ":" + port
 return str

print( connectURI("test.com", "800" ))
print( connectURI(port="8080", server="test.com"))
#http://test.com:800
#http://test.com:8080


#variable argument
def test(*args):
 print( type(args) )

test(1,2)
#


def union2(*ar):
 res = []
 for item in ar:
  for x in item:
   if not x in res:
    res.append(x)
 return res

print( union2( "HAM", "EGG", "SPAM" ) )
print( union2("GIR", "Generation", "gee") )
#['H', 'A', 'M', 'E', 'G', 'S', 'P']
#['G', 'I', 'R', 'e', 'n', 'r', 'a', 't', 'i', 'o', 'g']

def userURIBuilder(server, port, **user):
 str = "Http://" + server + ":" + port + "/?"
 for key in user.keys():
  str += key + "=" + user[key] + "&"
 return str

s = userURIBuilder("Test.com", "8080", id='userid', passwd='1234')
print(s)
#Http://Test.com:8080/?id=userid&passwd=1234&

#lambda function

g = lambda x, y : x*y
g(2,3)
#6

(lambda x: x*x)(3)
#9

#recursive function call
def factorial(x):
 if x == 1:
  return 1
 return x*factorial(x-1)

factorial(10)
#3628800

#pass
#while True:
# pass #control + c if you stop

class temp:
 pass


#help
def factorial(x):
 "return factorial of n, ...."
 if x == 1:
  return 1
 return x*factorial(x-1)

#help( factorial)
#return factorial of n, ....

#iterator
"""
for element in [1, 2, 3]:
for element in (1, 2, 3):
for key in {'one':1, 'two':2}:
for char in "123":
for line in open("myfile.txt") read line by line in the file
"""

#generator
def reverse(data):
 for index in range(len(data) -1, -1, -1):
  yield data[index]

for char in reverse('golf'):
 print( char)
#f
#l
#o
#g


#enumerate
for i, season in enumerate(['spring', 'summer', 'fall', 'winter']):
 print(i, season)
#0 spring
#1 summer
#2 fall
#3 winter

#iter
s='abc'
it = iter(s)
print(next(it))
#a
print(next(it))
#b
print(next(it))
#c
#print(next(it)) -> error