Python3 函數03
來自專欄 管道工的日常
def test(): a = 15 def test2(): print(a) return test2func = test()func()15--------------------------------------------------------------------------def spam(a, b, c, d): print(a, b, c, d)spam(1, 2, 3, 4)from functools import partials1 = partial(spam, c=55, d = 66)s1(1, 2)s2 = partial(spam, 1, 2)s2(66, 77)def s3(a, b, c=33, d = 99): spam(a, b, c, d)s3(1, 2, 3)1 2 3 41 2 55 661 2 66 771 2 3 99--------------------------------------------------------------------------from functools import partialfrom math import hypotpoints = [(1, 2),(3, 4),(5, 6),(7, 8)]def distance(p1, p2): x1, y1 = p1 x2, y2 = p2 return hypot(x2 - x1, y2 - y1)pt = (4, 5)points.sort(key=partial(distance, pt))print(points)[(3, 4), (5, 6), (1, 2), (7, 8)]-------------------------------------------------------------------------def func(name): def inner(age): print(name:, name, age:,age) return innerTom = func(tom)Tom(18)name: tom age: 18--------------------------------------------------------------------------class Animal(): def __init__(self, animal): self.animal = animal def sound(self, voice): print(self.animal,:,voice,...)dog = Animal(dog)dog.sound(wangwang)duck = Animal(duck)duck.sound(gaga)dog : wangwang ...duck : gaga ...-------------------------------------------------------------------------def voice(animal): def sound(voc): print(animal,:,voc,...) return sounddog = voice(dog)dog(wangwang)duck = voice(duck)duck(gaga)dog : wangwang ...duck : gaga ...------------------------------------------------------------------------def tag(tag_name): def add_tag(content): return <{0}>{1}</{0}>.format(tag_name, content) return add_tagcontent = Helloadd_tag = tag(a)print(add_tag(content))add_tag = tag(b)print(add_tag(content))<a>Hello</a><b>Hello</b>------------------------------------------------------------------------#人生關鍵的時間也就那幾年,錯過了也就錯過了。
推薦閱讀:
※Filedescriptor out of range in select
※學編程有哪些好點的網站?
※如何實現feed流
※如何學習編程語言0x02
※php 與C/C++ 集成的方法有哪些?