程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Practical Python tips without detours

編輯:Python

Hey, it's bad for all the big guys. Good duck ! I'm a panda

I'll bring you dry goods again !

This time I'm bringing you python Practical tips ~

Chain comparison operation

You may think that the process it performs is :1 < x, return True, Then compare True < 10, Of course, doing so is also a return True, Comparison expression True < 10, Because the interpreter will put True convert to 1,False convert to 0. But the chain comparison interpreter here does not do this internally , It will convert this chain comparison operation into :1 < x and x < 10, If you don't believe it, you can take a look at the last example . Such chained operation could have been owned by all programming languages , But unfortunately …(https://jq.qq.com/?_wv=1027&k=S5Y5AxaH)

enumeration

use enumerate Wrap an iteratable object , Iterations and indexes can be used at the same time , If you don't do that , Here is a more troublesome method :

enumerate You can also receive an optional parameter start, Default start be equal to 0.enumerate(list, start=1), such index The starting value of is 1

what are you having? python I won't answer the related error report 、 Or source code information / Module installation / Women's clothing bosses are proficient in skills You can come here :(https://jq.qq.com/?_wv=1027&k=2Q3YTfym) Or ask me at the end of the article


Generator object

You can assign the generator object to x, It means that you can be right x Iterate :

Its advantage is that there is no need to store intermediate results , Maybe you will use ( List push down ):(https://jq.qq.com/?_wv=1027&k=S5Y5AxaH)

It's faster than generator objects . relatively , The generator can save more memory , Its value is generated on demand , There is no need to save the whole result in memory like a list push down , At the same time, it cannot iterate again , The list pushdown is not the case .


iter() Acceptable callable Parameters

iter() There are two kinds of parameters received by built-in functions , The first is :

Parameters collection It must be an iteratable object or a sequence , The second is :

callable The function will always be called , Until its return result equals sentinel, for example :


Beware of variable default parameters

Instead, you should use a tag value to indicate “ Is not specified ” To replace variable values , Such as :(https://jq.qq.com/?_wv=1027&k=S5Y5AxaH)


Send the value to the generator function in

You can :


If you don't like using space indentation , Then you can use C Language curly braces {} Defined function :

 from __future__ import braces # there braces refer to :curly braces( Curly braces )
File "<stdin>", line 1
SyntaxError: not a chance

Of course, this is just a joke , I want to define a function with curly braces ? No way . Interested people can also learn about :

But this is python3 The features inside


Step size parameter in slice operation

There's another exception :x[::-1], Reverse list :

About inversion , There are two more functions reverse、reversed,reverse yes list Object method , no return value , and reversed It's a built-in method , Acceptable parameters include tuple、string、list、unicode, And user-defined types , Returns an iterator .


Decorator

Decorators wrap one function or method in another , You can add some additional functions to the wrapped functions , Like logs , You can also set parameters 、 Return the result to modify . The decorator is a little similar Java Medium AOP. The following example is a decorator that prints the parameters in the decorated function ,

@ It's grammar sugar , It is equivalent to :

for … else grammar

else The block of code will be in for Execute... After the normal end of the cycle , Unless you meet break( They don't execute ), It is equivalent to the following :

But this grammar looks strange , It feels like else Block is in for The statement block is executed when it is not executed , It's easy to compare if else The grammar of , If it's a else Switch to finally It may be easier to understand


python2.5 There is one __missing__ Method

dict If a subclass of defines a method __missing__(self, key), If key No longer dict in , that d[key] Will call __missing__ Method , and d[key] The return value of __missing__ The return value of .

stay collections There is a module called defaultdict Of dict Subclass , It is associated with missing Very similar , But for items that don't exist, you don't need to pass parameters .

I hope this article can be helpful to your study now or in the future

Learning to program (python) It is not difficult to

I'm a panda , See you in the next article (*◡‿◡)


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved