Home > Backend Development > Python Tutorial > Summarize what you need to pay attention to when coding in Python

Summarize what you need to pay attention to when coding in Python

零下一度
Release: 2017-06-27 09:56:02
Original
1247 people have browsed it


1. map, filter, reduce
1) map(func, input_list)
Apply the function to each element on the input list, such as:
input_list = [1, 2 , 3, 4, 5]


def pow_elem(x):
"""
Expand x to the power
:param x:
:return:
"""
return x * x


def multi_x_y(x, y):
return x * y


print map(pow_elem, input_list) # output:[1, 4, 9, 16, 25]

print map(multi_x_y, input_list, input_list) # output:[1, 4, 9, 16, 25]

2) filter(func_or_none, sequence)
Filter out the values ​​in the sequence that satisfy the function return True, and form a new sequence return, such as:
def is_odd(x):
"""
Determine whether x is an odd number
:param x:
:return:
"""
return True if x % 2 > 0 else False

print filter(is_odd, input_list ) # output: [1, 3, 5]

3) reduce(function, sequence)
reduce() function receives parameters similar to map(), a function f and a list, but the behavior Unlike map(), the function f passed to reduce() must receive two parameters. reduce() repeatedly calls function f for each element of the list and returns the final result value. For example: reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) is equivalent to ((((1+2)+3)+4)+5)
print reduce (lambda x, y: x * y, input_list) # output: 120

2. Ternary operation
The following two ways of writing are equivalent:
"Yes" if 2==2 else " No"
("No", "Yes")[2==2]
That is:
1) condition_is_true if condition else condition_is_false
2) (if_test_is_false, if_test_is_true)[test]## Both #1) and 2) can implement ternary operations, but 2) is rarer and less elegant. At the same time, 2) is not a short-circuit operation, as follows:
5 if True else 5/0 # output: 5
(1/0, 5)[True] # throw exception-> ZeroDivisionError: integer division or modulo by zero

3、Decorator

1) In Python, we can define it inside the function function and call it, such as:
def hi(name="patty"):
print("now you are inside the hi() function")

def greet():

return "now you are in the greet() function"

def welcome():

return "now you are in the welcome() function"

print(greet())

print(welcome())
print("now you are back in the hi() function")
The output result is:
now you are inside the hi() function
now you are in the greet() function
now you are in the welcome() function
now you are back in the hi() function

2) You can also return the internal function and use the external Function is called, such as:

def hi(name="patty"):
def greet():
return "now you are in the greet() function"

def welcome ():

return "now you are in the welcome() function"

return greet if name == 'patty' else welcome

print hi() # < ;function greet at 0x109379a28>
print hi()() # now you are in the greet() function
In the above code, the hi() call returns a function object, which can be obtained from the if/else statement It is judged that what is returned is the greet() function. When we call hi()(), we actually call the internal function greet().

3) Pass the function as a parameter to another function, such as:

def hi():
return "hi patty!"

def doSomethingBeforeHi(func):

print("I am doing some boring work before executing hi()")
print(func())

doSomethingBeforeHi(hi)

Output result:
I am doing some boring work before executing hi()
hi patty!
So far, we have implemented a simple decorator, which outputs a line before calling the hi() function. In actual applications, it may be some preprocessing operations. In fact, the function of the decorator is to add some common functions before and after your core logic is executed.

4) Implementation of simple decorator

def a_new_decorator(a_func):

def wrapTheFunction():

print("I am doing some boring work before executing a_func() ")

a_func() # call this function

print("I am doing some boring work after executing a_func()")

return wrapTheFunction

def a_function_requiring_decoration():

print("I am the function which needs some decoration to remove my foul smell")

a_function_requiring_decoration()

#outputs: "I am the function which needs some decoration decoration to remove my foul smell"

a_function_requiring_decoration = a_new_decorator(a_function_requiring_decoration)

#now a_function_requiring_decoration is wrapped by wrapTheFunction()

a_function_requiring_decoration()
# I am doing some boring work before executing a_func()
# I am the function which needs some decoration to remove my foul smell
# I am doing some boring work after executing a_func()

5) Annotation form
@a_new_decorator
def b_function_requiring_decoration():
print("I am the another function which needs some decoration to remove my foul smell")

b_function_requiring_decoration()
# I am doing some boring work before executing a_func()
# I am the another function which needs some decoration to remove my foul smell
# I am doing some boring work after executing a_func()
Here @a_new_decorator is equivalent to a_new_decorator(b_function_requiring_decoration)

6) Get name
For a_function_requiring_decoration in 4), we print print(a_function_requiring_decoration.__name__) and get The result is wrapTheFunction, but actually what we want to get is the a_function_requiring_decoration function name corresponding to a_func. Python provides us with wraps to solve this problem.
from functools import wraps
def a_new_decorator(a_func):
@wraps(a_func)
def wrapTheFunction():
print("I am doing some boring work before executing a_func()" )

a_func()

print("I am doing some boring work after executing a_func()")

return wrapTheFunction

7) Decorator Some application scenarios
User authentication
def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
auth = {"username": "patty", "password": "123456"}
if not check_auth(auth['username'], auth['password']):
authenticate()
return f(*args, * *kwargs)

def check_auth(username, password):
print "Starting check auth..."
return True if (username == 'patty' and password == '123456') else False


def authenticate():
print "Already authenticate"
return decorated

@requires_auth
def welcome():
return " Welcome patty!"

print welcome()

logging
def logit(func):
@wraps(func)
def with_logging(*args, ** kwargs):
print(func.__name__ + " was called")
return func(*args, **kwargs)
return with_logging

@logit
def addition_func(x ):
"""Do some math."""
return x + x


result = addition_func(4)
will print: addition_func was called

8) Decorator with parameters
from functools import wraps

def logit(logfile='out.log'):
def logging_decorator(func):
@wraps(func )
def wrapped_function(*args, **kwargs):
log_string = func.__name__ + " was called"
print(log_string)
Open the logfile and append
with open( logfile, 'a') as opend_file:
#Now we log to the specify logfile


##Afile.write (log_string + '\ n')

Return wrapped_function
RETURN LOGGING_DECORTOR
## @logit()

def myfunc1():

pass

myfunc1()

# Output: myfunc1 was called

# A file called out.log now exists, with the above string

@logit(logfile='func2.log')

def myfunc2():

pass

myfunc2()

9) Use the class as Decorator
import os
class Logit(object):

def __init__(self, log_file):

self.log_file = log_file

def __call__(self, func):
with open(self.log_file, 'a') as fout:
log_msg = func.__name__ + " was called"
fout.write(log_msg)
fout.write(os.linesep)

# Now, send a notification

self.notify()

def notify(self):

# logit only logs, no more### pass###

class EmailLogit(Logit):
'''
A logit implementation for sending emails to admins
when the function is called.
'''
def __init__(self, log_file , email='admin@myproject.com'):
self.email = email
super(EmailLogit, self).__init__(log_file)

def notify(self):
# Send an email to self.email
#Will not be Implemented here
completely Open (Self.log_file, 'A) as f:
F.write ("do something ...")
#                                                                                                           ("log1.txt")
def myfunc3():
pass

@EmailLogit("log2.txt")
def myfunc4():
pass
Use Using classes as decorators makes our code look more concise, and it can also achieve personalization and reuse of functions through inheritance.

4. Variable types
Variable types in Python include lists and dictionaries. The elements in these objects are changeable, such as
>>> foo = ['hi ']
>>> foo += ['patty']

>>> foo

['hi', 'patty']
>>> foo[0]='hello'
>>> foo
['hello', 'patty']

>>> fdict = {"name":" patty"}
>>> fdict.update({"age":"23"})
>>> fdict
{'age': '23', 'name ': 'patty'}

>>> fdict.update({"age":"25"})

>>> fdict
{'age': '25' , 'name': 'patty'}

In the method, if the incoming parameter adopts a variable type and is assigned a default value, please note that the following situations will occur:
>>> def add_to(num, target=[]):
... target.append(num)
... return target

...

>>> add_to(1)
[1]
>>> add_to(2)
[1, 2]
>>> add_to(3)
[1, 2, 3]
This is because the default parameters are calculated when the method is defined, rather than calculated again each time it is called. Therefore, in order to avoid the above situation, when we expect to calculate with a new empty list every time the method is called, we can write it as follows:
>>> def add_to(num, target= None):
... if target is None:
... target = []
... target.append(num)
... return target
...
>>> add_to(1)
[1]
>>> add_to(2)
[2]

5. Shallow copy and deep copy Copy
In Python, there are differences between object assignment and copy (deep/shallow copy). If you don’t pay attention when using it, unexpected results may occur.
1) The default in Python is shallow copying
>>> foo = ['hi']

>>> bar = foo

>>> id (foo)
4458211232
>>> id(bar)
4458211232
>>> bar.append("patty")
>>> bar
['hi', 'patty']
>>> foo
['hi', 'patty']
Note: id(foo)==id(bar) , indicating that foo and bar refer to the same object. When the append operation is performed on the list through the bar reference, the output of foo is consistent with that of bar because it points to the same memory space.

2) Deep copy
>>> foo
['hi', {'age': 20, 'name': 'patty'}]
>> ;> import copy

>>> slow = copy.deepcopy(foo)

>>> slow
['hi', {'age': 20, 'name' : 'patty'}]
>>> slow[0]='hello'
>>> slow
['hello', {'age': 20, 'name ': 'patty'}]
>>> foo
['hi', {'age': 20, 'name': 'patty'}]
Note: Since slow is correct The deep copy of foo actually opens a new space in the memory and copies the content referenced by the foo object to the new memory space. Therefore, when the content referenced by the slow object is updated, the changes are only reflected On the reference of the slow object, the content referenced by the foo object has not changed.

6. Collection
1) defaultdict
For ordinary dict, if you obtain a non-existent key, a KeyError error will be triggered, as follows:
some_dict = {}
some_dict['colours' ]['favourite'] = "yellow"
# Raises KeyError: 'colours'
But through defaultdict, we can avoid this situation, as follows:
import collections
import json
tree = lambda: collections.defaultdict(tree)
some_dict = tree()
some_dict['colours']['favourite'] = "yellow"
print json.dumps(some_dict)
# Works fine, output: {"colours": {"favourite": "yellow"}}

2) OrderedDict
OrderedDict can print out the dictionary in the key order when we define the dictionary and change the value. The values ​​will not change the order of the keys. However, after the keys are deleted and reinserted, the keys will be reordered to the end of the dict.
from collections import OrderedDict

colours = OrderedDict([("Red", 198), ("Green", 170), ("Blue", 160)])
for key, value in colours.items():
print(key, value)

3) Counter
Use Counter to count the number of occurrences of specific items, such as:
from collections import Counter

colours = (
('Yasoob', 'Yellow'),
('Ali', 'Blue'),
('Arham', 'Green'),
( 'Ali', 'Black'),
('Yasoob', 'Red'),
('Ahmed', 'Silver'),
)

favs = Counter(name for name, color in colours)
print(favs)
# Counter({'Yasoob': 2, 'Ali': 2, 'Arham': 1, 'Ahmed': 1})

4) deque
deque is a double-ended queue, which can be inserted and deleted at the head and tail respectively, as follows:
from collections import deque
queue_d = deque()
queue_d.append( 1)
queue_d.append(2)
print queue_d # deque([1, 2])
queue_d.appendleft(3)
print queue_d # deque([3, 1, 2])

queue_d.pop()
print queue_d # deque([3, 1])
queue_d.popleft()
print queue_d # deque([1])

deque can set the maximum length of the queue. When the number of elements exceeds the maximum length, the corresponding number of elements will be deleted from the opposite direction of the current insertion direction, as follows:
queue_c = deque(maxlen=5, iterable=[2, 4, 6])
queue_c.extend([7, 8])
print queue_c # deque([2, 4, 6, 7, 8], maxlen=5)
queue_c.extend([ 10, 12])
print(queue_c) # deque([6, 7, 8, 10, 12], maxlen=5)
queue_c.extendleft([18])
print(queue_c) # deque([18, 6, 7, 8, 10], maxlen=5)

5) nametuple
tuple is an immutable list. The elements in the tuple cannot be reassigned. We can only Access the elements in the tuple through index. The nametuple can be regarded as an immutable dictionary, and the elements in the tuple can be accessed through name. For example:
from collections import namedtuple

Animal = namedtuple('Animal', 'name age type')
perry = Animal(name="perry", age=31, type="cat ")

print(perry)
# Output: Animal(name='perry', age=31, type='cat')

print(perry.name)
# Output: 'perry'

print(perry[0])
# Output: 'perry'

print(perry._asdict())
# Output: OrderedDict ([('name', 'perry'), ('age', 31), ('type', 'cat')])

7、Object introspection
1) dir: List the All methods of the object
2) type: returns the type of the object
3) id: returns the id of the object

##8, generator

1)list
>>> ; squared = [x**2 for x in range(10)]
>>> squared
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
2) dict
{v: k for k, v in some_dict.items()}
3) set
>>> squared = {x**2 for x in range (10)}
>>> squared
set([0, 1, 4, 81, 64, 9, 16, 49, 25, 36])

9. Exception Processing

try:
print('I am sure no exception is going to occur!')
except Exception:
print('exception')
else:
# any code that should only run if no exception occurs in the try,
# but for which exceptions should NOT be caught
print('This would only run if no exception occurs. And an error here '
'would NOT be caught.')
finally:
print('This would be printed in every case.')

# Output: I am sure no exception is going to occur!

# This would only run if no exception occurs.
# This would be printed in every case.
The statements in else will be executed before finally.

10. Built-in method

a_list = [[1, 2], [3, 4], [5, 6]]
print(list(itertools.chain.from_iterable(a_list)) )
# Output: [1, 2, 3, 4, 5, 6]

# or

print(list(itertools.chain(*a_list)))
# Output: [1, 2, 3, 4, 5, 6]


class A(object):
def __init__(self, a, b, c, d, e, f):
self.__dict__.update({k: v for k, v in locals().items() if k != 'self'})

11. for-else statement
There are two normal ways to end the for statement: one is when specific conditions are met The next break is to jump out of the loop, and the second is to end all conditional loops. The else statement in for-else will only be executed when all conditions have been judged and the for loop ends normally, as follows:
for x in range(1, 10, 2):
if x % 2 == 0:
print "found even of %d"%x
break
else:
print "not foud even"
# output: not foud even

12. Compatible with Python 2+ and Python 3+
1) Use the __future__ module to reference Python 3+ modules in the Python 2+ environment
2) Compatible module import method
try:
import urllib.request as urllib_request # for Python 3
except ImportError:
import urllib2 as urllib_request # for Python 2

Reference:

The above is the detailed content of Summarize what you need to pay attention to when coding in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template