This article is the 25th day article of Python Advent Calendar 2016. A lot of fear is the last day !!
I usually write for the server side as an engineer while representing a company called Liaro. This is a continuation of Tips② that I wrote a long time ago, but please understand that it is not as much as before due to various chasers. I think that there are many parts written with ambiguous knowledge, so thank you for your cooperation!
Although it is another language, Python also has map / filter / reduce, which is provided in the lambda of an anonymous function (lambda expression).
python:Python3.5.2
>>> f = lambda x:x**2
>>> f(3)
9
>>>
>>> a = [i for i in range(10)]
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> b = map(lambda x:x*x, a)
>>> b = map(lambda x:x*x, a)
>>> for i in b:
... print(i)
...
0
1
4
9
16
25
36
49
64
81
>>> c = filter(lambda x:x%2==0, a)
>>> for i in c:
... print(i)
...
0
2
4
6
8
>>> from functools import reduce #import reduce
>>> d = reduce(lambda x,y:x+y, a)
>>> d
45
>>>
Python has a function called a decorator, and you can extend the method like @ hoge
above the method name (here is detailed. ).
The example of the link destination is good, and other than that, it will be similar without thinking much,
Suppose you write the following when extending a function called foo.
python:Python3.5.2
>>> def foo(x, y):
... return x ** y
...
>>>
>>> def hoge(func):
... def inner(x, y):
... if x == y == 0:
... return "Indefinite"
... return func(x, y)
... return inner
...
>>>
>>> foo = hoge(foo)
In Python, it can be rewritten as follows using the @
symbol.
python:Python3.5.2
>>> @hoge
... def foo(x, y):
... return x ** y
...
>>>
For example, a method in a class is basically an instance method, but writing @ classmethod
makes it a class method, and writing @staticmethod
makes it a static method.
In the previous Tips, I wrote about the handling of arguments in basic functions, but I didn't write some notes, so I'd like to write them here.
python:Python3.5.2
>>> def foo(x, y):
... return x ** y
...
>>> foo(y=5, x=2) #Name matching takes precedence over order for arguments
32
>>>
>>> def hoge(a=[]):
... a.append(1)
... return a
...
>>> b = hoge()
>>> print(b)
[1]
>>> b = hoge()
>>> print(b) #The default value is not generated every time you call
[1, 1]
This is a Python coding convention. Python has recommended coding conventions for the language and I think many Python users follow them. Formerly called PEP8, it has changed. However, there is basically no problem because people suddenly became spicy in English and came out with PEP8 (PEP8). To enumerate some ・ Indent is 4 spaces ・ The length of one line is basically 79 characters -Write variables and functions in lowercase (snake) and class names in camel ・ Import separates lines And so on. I think you should read it once!
The content is very thin, but I want to write a sequel again ... Then Merry Xmas !! & Have a nice year ~~! !! !! !! !! !! !!
Click here for Tips ① Click here for Tips②
Recommended Posts