1、 understand Pythonic Concept


Tim Peters Of 《The Zen of Python》 Believe to learn Python I'm familiar with everything , Input in interactive environment import this You can see , What's really interesting is that Python The source code of Zen :

d = {}
for c in (65, 97):
for i in range(26):
d[chr(i+c)] = chr((i+13) % 26 + c) print "".join([d.get(c, c) for c in s])

The book also gives an example of fast scheduling :

def quicksort(array):
less = []
greater = []
if len(array) <= 1:
return array
pivot =array.pop()
for x in array:
if x <= pivot:
return quicksort(less) + [pivot] + quicksort(greater)

8、 utilize assert Statements to find problems

>>> y = 2
>>> assert x == y, "not equals"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError: not equals
>>> x = 1
>>> y = 2
# The above code is equivalent to
>>> if __debug__ and not x == y:
... raise AssertionError("not equals")
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
AssertionError: not equals

Join at runtime -O Parameter can disable assertion .

 9、 Intermediate variables are not recommended for data exchange
x, y = 1, 2
x, y = y, x

principle : Right values create tuples , The lvalue receives the corresponding element of the tuple .

10、 make the best of Lazy evaluation Characteristics of

def fib():
a, b = 0, 1
while True:
yield a
a, b = b, a + b

11、 Understand the pitfalls of enumerating alternative implementations

utilize Python The dynamic characteristics of can realize enumeration :

# Mode one 
class Seasons:
Spring, Summer, Autumn, Winter = range(4)
# Mode two
def enum(*posarg, **keysarg):
return type("Enum", (object,), dict(zip(posarg, range(len(posarg))), **keysarg))
Seasons = enum("Spring", "Summer", "Autumn", Winter=1)
# Mode three
>>> from collections import namedtuple
>>> Seasons = namedtuple('Seasons', 'Spring Summer Autumn Winter')._make(range(4))
>>> Seasons.Spring
# But it is unreasonable to implement enumeration in the above way
>>> Seasons._replace(Spring=2) │
Seasons(Spring=2, Summer=1, Autumn=2, Winter=3)
# Python3.4 Added enumeration , Inheritance is allowed only if the parent class does not have any enumeration members

12、 It is not recommended to use type To do type checking 、 While using isinstance()

14、 alert eval() Security vulnerability

# Use it properly 
>>> eval("1+1==2")
>>> eval('"a"+"b"')
# Bad hearted geek
>>> eval('__import__("os").system("dir")')
Desktop Documents Downloads examples.desktop Music Pictures Public __pycache__ Templates Videos
>>> eval('__import__("os").system("del * /Q")') # Hey, hey, hey

If you do need to use eval, It is suggested to use the one with better safety ast.literal_eval.

19、 Use... Sparingly from ... import sentence
Python There are three ways to introduce external modules :import sentence 、from...import Statements and __import__ function , among __import__ Function explicitly passes the name of the module as a string and assigns it to a variable in the namespace .
Use import The following points need to be noted :
priority of use import a In the form of
Use... Sparingly from a import A
Avoid using from a import *
Why? ? Let's see Python Of import Mechanism ,Python When initializing the runtime environment, a batch of built-in modules will be loaded into memory in advance , At the same time, the relevant information is stored in sys.modules in , We can go through sys.modules.items() View preloaded module information , When loading a module , The interpreter actually does the following :
stay sys.modules Search for the existence of the module in , If it exists, import it into the current local namespace , If it doesn't exist, create a dictionary object for it , Insert into sys.modules in
Before loading, confirm whether you need to compile the file corresponding to the module , Compile if necessary
Perform dynamic loading , Execute the compiled bytecode in the current namespace , And put all the objects in the dictionary corresponding to the module 
>>> dir()
['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']
>>> import test
testing module import
>>> dir()
['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'test']
>>> import sys
>>> 'test' in sys.modules.keys()
>>> id(test)
>>> id(sys.modules['test'])
>>> dir(test)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'a', 'b']
>>> sys.modules['test'].__dict__.keys()
dict_keys(['__file__', '__builtins__', '__doc__', '__loader__', '__package__', '__spec__', '__name__', 'b', 'a', '__cached__'])

And you can see that , For user-defined modules ,import The mechanism will create a new module Add it to the current local namespace , At the same time sys.modules Also add the information of the module , But it's essentially referring to the same object , adopt test.py There will be one more bytecode file in the directory .

20、 priority of use absolute import To import modules

23、 Use else Clause simplify loop ( Handling exceptions )

Python Of else Clause provides an implicit answer to whether the loop is caused by break Statement raises the judgment of the end of the loop

>>> def print_prime(n):
... for i in range(2, n):
... for j in range(2, i):
... if i % j == 0:
... break
... else:
... print('{} is a prime number'.format(i))
>>> print_prime(7)
2 is a prime number
3 is a prime number
5 is a prime number

It can be seen that ,else Clause is executed when the loop ends normally and the loop condition does not hold , from break Statement is not executed when it is interrupted , Again , We can use this grammar sugar to act on while and try...except in .

31、 Remember that function parameters are neither values nor references

The right way to say it is to pass it on to (call by object) Or a reference to an object (call-by-object-reference), Function parameters pass in the entire object , Modifications to mutable objects are visible both outside and inside the function , Research on immutable objects ” modify “ It is often realized by generating a new object, but it is realized by assignment .

39、 Use Counter Count statistics
Common counting statistics can be used dict、defaultdict、set and list, however Python Provides a more elegant way :
>>> from collections import Counter
>>> some_data = {'a', '', 2, 3, 5, 'c', '', 4, 5, 'd', 'b'}
>>> Counter(some_data)
Counter({'',: 1, 2: 1, 3: 1, 4: 1, 5: 1, '': 1, 'b': 1, 'a': 1, 'd': 1, 'c': 1})

Counter Class is a subclass of dictionary class , It's a container object , Used to count hash objects , Support +、-、&、|, among & and | Return two Counter The minimum and maximum values of the elements of an object .

# initialization 
Counter(s=3, c=2, e=1, u=1)
Counter({'s': 3, 'c': 2, 'u': 1, 'e': 1})
# Common methods
list(Counter(some_data).elements()) # obtain key value
Counter(some_data).most_common(2) # front N The element with the highest frequency of occurrence and the corresponding number of times
(Counter(some_data))['y'] # Accessing nonexistent elements returns 0
c = Counter('success')
c.update('successfully') # Update Statistics
c.subtract('successfully') # Subtracting Statistics , Allow for 0 Or negative

41、 Use argparse Handling command line arguments

import argparse
parse = argparse.ArgumentParser()
parse.add_argument('-o', '--output')
parse.add_argument('-v', dest='verbose', action='store_true')
args = parser.parse_args()

42、 Use pandas Handling large CSV file     course

reader(csvfile[, dialect='excel'][, fmtparam]) # Read a csv file , Return to one reader object 
csv.writer(csvfile, dialect='excel', **fmtparams) # write in csv file
csv.DictWriter(csvfile, fieldnames, restval='', extrasaction='raise', dialect='excel')

Commonly used API

43、 In general use ElementTree analysis XML      course

count = 0
for event, elem in ET.iterparse('test.xml'):
if event == 'end':
if elem.tag == 'userid':
count += 1

45、 Use traceback Get stack information

When something goes wrong , Developers often need to see live information ,trackback Modules can meet this requirement

traceback.print_exc() # Type of printing error 、 Value and specific trace Information 
traceback.print_exception(type, value, traceback[, limit[, file]]) # The values of the first three parameters can be obtained from sys.exc_info()
raceback.print_exc([limit[, file]]) # ditto , You don't need to pass in so many parameters
traceback.format_exc([limit]) # Same as print_exc(), The returned string is
traceback.extract_stack([file, [, limit]]) # Extract... From the current stack trace Information

traceback The module gets exception related data through sys.exc_info() Got , This function returns the exception type type、 abnormal value、 Call and stack information traceback A tuple of components .

meanwhile inspect The module also provides access to traceback Object's interface .

50、 Using module to realize single instance mode

51、 use mixin Patterns make programs more flexible

Template method pattern is to define the skeleton of an algorithm in a method , And delay some implementation steps to subclasses . Template method can make subclasses without changing the algorithm structure , Redefine some of the steps in the algorithm . Look at an example :

class People(object):
def make_tea(self):
teapot = self.get_teapot()
return teapot

obviously get_teapot() Methods don't need to be predefined , In other words, our base class does not need to declare abstract methods in advance , Subclasses just need to inherit People Class and Implementation get_teapot(), This makes it easy to debug code . But then we thought about if a subclass StreetPeople It's about people walking in the street , Then this class will not implement get_teapot(), One call make_tea() It's like you can't find get_teapot() Of AttributeError, So programmers should immediately think of , As the demand increases , More and more People Subclass will choose to drink coffee instead of tea , Or a cigar or something , According to the above ideas , Our code will only become harder to maintain .

So we want to be able to dynamically generate different instances :

class UseSimpleTeapot(object):
def get_teapot(self):
return SimpleTeapot() class UseKungfuTeapot(object):
def get_teapot(self):
return KungfuTeapot() class OfficePeople(People, UseSimpleTeapot): pass class HomePeople(People, UseSimpleTeapot): pass class Boss(People, UseKungfuTeapot): pass def simple_tea_people():
people = People()
people.__base__ += (UseSimpleTeapot,)
return people def coffee_people():
people = People()
people.__base__ += (UseCoffeepot,) def tea_and_coffee_people():
people = People()
people.__base__ += (UseSimpleTeapot, UserCoffeepot,)
return people def boss():
people = People()
people.__base__ += (KungfuTeapot, UseCoffeepot, )
return people

The principle of the above code is that each class has a __bases__ attribute , It's a tuple , Used to store all the base classes , As a dynamic language ,Python The base class in can be changed dynamically during operation . So when we add a new base class to it , This class has new methods , That's mixing in mixin.

With this technology, we can complete the requirements without modifying the code :
import mixins # Define employee needs as Mixin Put in mixins modular
def staff():
people = People()
bases = []
for i in config.checked():
bases.append(getattr(maxins, i))
people.__base__ += tuple(bases)
return people

52、 Using publish subscribe mode to achieve loose coupling

Publish subscribe mode is a programming mode , The sender of a message does not send its message to a specific receiver , Instead, the published messages are divided into different categories and published directly , It's not about who the subscribers are . And subscribers can be interested in one or more categories , And only receive messages of interest , And don't care which publisher posted the news . To achieve this pattern , You need an intermediary agent Broker, It maintains the relationship between publishers and subscribers , The subscriber tells it the subject of interest , And the publisher's information is also routed to each subscriber through it .

from collections import defaultdict
route_table = defaultdict(list)
def sub(topic, callback):
if callback in route_table[topic]:
route_table[topic].append(callback) def pub(topic, *args, **kw):
for func in route_table[topic]:
func(*args, **kw)


Put the above code in Broker.py Module , All kinds of parameter detection are omitted 、 priority 、 The need to unsubscribe , Just show us the basic implementation of publish subscribe mode :

import Broker
def greeting(name):
print('Hello, {}'.format(name))
Broker.sub('greet', greeting)
Broker.pub('greet', 'LaiYonghao')

Study attention blinker and python-message Two modules .


To improve Python programmatic 91 More related articles with suggestions

  1. To improve Python programmatic 91 A suggestion 2

       62.metaclass stackflow           Chinese translation    63.Python Object protocol   https://zhuanlan.zhihu.com/p/26760180     ...

  2. 《 Write quality code : improve Python programmatic 91 A suggestion 》 Journal entry

    Write quality code : improve Python programmatic 91 A suggestion  http://book.douban.com/subject/25910544/ 1.( Suggest 16)is Used to judge two objects id Whether it is equal or not ,== It's judgment ...

  3. Let's make some improvements Python programmatic 91 A suggestion

    Read a good book 「 Write high quality code to improve Python programmatic 91 A suggestion 」, Most of the suggestions are really good , I write python Also have 3 More than years , But there are some places that I haven't noticed , I made a special arrangement , For your reference . I have already ...

  4. 《 improve python programmatic 91 A suggestion 》 Reading notes

    recommend < improve Pthon programmatic 91 A suggestion > From the basic principles . Conventional method . grammar . library . Design patterns . Internal mechanism . Development tools and performance optimization 8 In depth discussion on three aspects of writing high quality python Code skills . Taboos and best practices . Reading is like ...

  5. Write quality code improve Python programmatic 91 A suggestion ( post-reading Notes )

    I bought this book a long time ago , I always thought Python There are all kinds of trick, Always have to fall into the trap , After reading some materials, I found this book , I feel very good , Unfortunately, I always have too many chores , When you can't find the whole piece ...

  6. improve Python programmatic 91 A suggestion

    Suggest 1. understand Pythonic Concept —- See Python Medium <Python zen > Suggest 2. To write Pythonic Code (1) Avoid irregular code , For example, only case sensitive variables . Use confusing variable names . Fear of being too long ...

  7. improve Python programmatic 91 A suggestion

    1. Introduction Suggest 1: understand Pythonic Concept —- See Python Medium <Python zen > Suggest 2: To write Pythonic Code Avoid irregular code , For example, only case sensitive variables . Use confusing variable names . Fear ...

  8. Reading notes --《 Write quality code : improve Python programmatic 91 A suggestion 》

      Chapter one Introduction Suggest to write Pythonic The code of the formula , I understand it as making the most of pythonAPI, Write the code in the simplest way 1. Two variables exchange : a, b = b, a 2. Flip list: a = [1, 2, 3, ...

  9. Write quality code -- improve python Suggestions for procedures ( 6、 ... and )

    The original text is published on my blog home page , Reprint please indicate the source ! Suggestion 28 : Differentiate between mutable and immutable objects python Everything is an object , Each object has a unique identifier (id()). type (type()) And the value of , Object can be fixed according to its value ...

Random recommendation

  1. GetViewUrl

    <f:TemplateField HeaderText=" name " ColumnID="Name" ExpandUnusedSpace="true ...

  2. C Variables in language

    1. Computers need to process data 2. Data needs to be stored in memory 3. Computers can only recognize 0 perhaps 1 Binary data of 4. What we see , All the data used are stored in binary in the computer 5. The same... In memory 01 binary data , In different ways ...

  3. IOS Develop some common addresses

    1. Developer center https://developer.apple.com/membercenter/index.action 2.itunesconnect https://itunesconnect. ...

  4. Apache Spark Read the source code 18 -- Use Intellij idea debugging Spark Source code

    Welcome to reprint , Reprint please indicate the source , Huihu Ichiro . Summary The last blog post described how to view the call stack by modifying the source code , Even though it's practical , But every time you modify it, you need to compile , It took a lot of time , The efficiency is not high , And it's an invasive modification , Not elegant . This article is about how to use ...

  5. 2015 The best of the year 10 individual JavaScript frame

    JavaScript Is the most popular front-end development programming language . It's for WEB Developers provide the ability to design products with rich functions . Clean user interface WEB Ability to apply .JavaScript The framework makes WEB Application design becomes simple , And it can provide a lot of ...

  6. IDL Create array

    1. Assignment creation Through square brackets [] Assign values to create arrays , The sample code is as follows IDL> arr=[1,2,3] IDL> help,arr ARR INT = Array[3] IDL> arr=[[1, ...

  7. 1、MyEclipse Plug in configuration and through MyEclipse Generate table corresponding to JPA Code

     Remove MyEclipse The way to plug in is to open :WindowàCustomize Perspective Window for plug-in configuration : Take out the plug-ins that are not commonly used in the figure below , Finally click OK. 3. Click on OK The effect picture is shown as follows : ...

  8. zabbix Flip-flop expression

    zabbix Flip-flop expression Triggers use logical expressions to evaluate passing item What kind of state is the acquired data in , Expressions in triggers are very flexible , We can create a complex logic test monitor , The trigger expression has the following form : {<serve ...

  9. Properties of a scheduled job in Laravel

    Every entry you add is converted into an instance of Illuminate\Console\Scheduling\Event and stored ...

  10. In a linux Environmental execution io Operation of the bug

    Today, the project has a wonderful request ... What is it? After uploading the video in the background , Analyze the duration , And preview , And spliced together , However , It's not too much trouble , It was written very quickly , There is no problem after the local test , Um. , After publishing to the test environment , One jar The package reported an error ...