021_func

#!/usr/bin/env python
# Author:liujun

def func1(): #This is a function
"""ttest...."""
print("in the func1")
return 0

def func2(): #This is a process
"""testing2....."""
print("in the func2")


x = func1()
y = func2()
print("x:%s,y:%s"%(x,y))

# The difference between function and process
# A process is a function returning nothing



def test6():
loggin()
#test6() #NameError: name 'loggin' is not defined
def loggin():
print("loogin")











#!/usr/bin/env python
# Author:liujun

import time

def loggin():
with open("test.bak","a") as f:
time_format = "%Y-%m-%d-%X"
time_current = time.strftime(time_format)
f.write("%s end action "%time_current)

def test1():
print("in the test1")
loggin()

def test2():
print("in the test2")
loggin()

def test3():
print("in the test3")
loggin()

test1()
test2()
test3()










#!/usr/bin/env python
# Author:liujun

def test1():
print("in the test1")

def test2():
print("in the test2")
return 0

def test3():
print("in the test3")
return 1,"hello",["Alex","WuPeiQi"],{"name":"alix"}

print(test1())
print(test2())
print(test3())


'''
in the test1
None
in the test2
0
in the test3
(1, 'hello', ['Alex', 'WuPeiQi'], {'name': 'alix'})
'''










#!/usr/bin/env python
# Author:liujun

def test(x,y):
print(x)
print(y)

test(1,7)
# Positional arguments : the formal parameters correspond to the real parameters.
test(y=7,x=1)
# Keyword arguments : the order of the real parameters is independent of the order of the formal parameters.

#test(1,x=7) #TypeError: test() got multiple values for argument 'x'
test(1,y=7) # You got it right
# test(x=1,7) #SyntaxError: positional argument follows keyword argument
# test(y=1,7) #SyntaxError: positional argument follows keyword argument


def test1(x,y,z):
print(x)
print(y)
print(z)

#test1(1,2,x=1) #TypeError: test1() got multiple values for argument 'x'
#test1(1,2,y=1) ##TypeError: test1() got multiple values for argument 'y'
test1(1,2,z=1)# You got it right
#test1(1,x=1,y=2) #TypeError: test1() got multiple values for argument 'x'
test1(1,y=1,z=2) #You got it right
#test1(x=1,6,z=9) #SyntaxError: positional argument follows keyword argument


def test2(x,y,z=6):
print(x)
print(y)
print(z)
#test1(y=1,z=2)#TypeError: test1() missing 1 required positional argument: 'x'

def test3(*args): # Parameters are passed to the func in the way of tuple
print(args)
test3(1,2,3,4,5,6)
test3(*[1,2,3,4,5,6]) #args=tuple([1,2,3,4,5,6])

def test4(x,*args):
print(x)
print(args)

def test5(**kwargs): # Parameters are passed in the way of dict
print(kwargs)
test5(name="liujun",age=100,sex="man")
test5(**{"name":"llll","age":10000})















#!/usr/bin/env python
# Author:liujun


def chargeName(name):
print("before change:",name)
name = "mingming" # local variable
print("after change:",name)

name = "liujun"
chargeName(name)
print(name) # liujun


v = "global"
def test1():
print(v) # 'v' is the global variable above
test1()

def test2():
v = "local" # Define a local variable named 'v'
print(v) # the local variable
test2()

def test3():
global v
v = 100 # Change the global variable
print(v)
test3()
print(v)


def test4():
global name;
name = "liunun"
test4()
print(name)


names = ["Alix","jack","Rain"]
def test5():
names[0] = "jjjjjjj" # Change the global variable
test5()
print(names)








#!/usr/bin/env python
# Author:liujun

def add(a,b,f):
return f(a) + f(b)

res = add(3,-6,abs)
print(res)




原文地址:https://www.cnblogs.com/liujun5319/p/9600670.html