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

Python Advanced Series (III)

編輯:Python

Catalog

Ternary operator

Decorator

Everything is the object

Define a function in a function


Ternary operator

Ternary operators are usually used in Python Is called a conditional expression , These expressions are based on true (true)/ false (not) Condition judgment of , stay Python 2.4 That's the ternary operation .

Here is a pseudocode and an example :

Pseudo code :

# If the condition is true , Return to true Otherwise return false
condition_is_true if condition else condition_is_false

Example :

is_fat = True
state = "fat" if is_fat else "not fat"

It allows you to quickly judge with a simple line , Instead of using complex multilines if sentence . This is very useful most of the time , And it can make the code simple and maintainable .

Another obscure usage is rare , It uses tuples , Please continue to watch :

Pseudo code :

#( Return to leave , Return to true )[ True or false ]
(if_test_is_false, if_test_is_true)[test]

Example :

fat = True
fitness = ("skinny", "fat")[fat]
print("Ali is ", fitness)
# Output : Ali is fat

This is why it works , Because in Python in ,True be equal to 1, and False be equal to 0, This is equivalent to using... In tuples 0 and 1 To select data .

The above example is not widely used , and Python Players generally don't like that , Because no Python taste (Pythonic). This usage makes it easy to compare real data with true/false Muddle up .

Another reason for not using tuple conditional expressions is that both conditions are executed in tuples , and if-else The conditional expression for does not do this .

for example :

condition = True
print(2 if condition else 1/0)
# Output : 2
print((1/0, 2)[condition])
# Output ZeroDivisionError abnormal 

This is because data is created first in tuples , And then use True(1)/False(0) To index data . and if-else The conditional expression follows the normal if-else Logic tree , therefore , If the condition in the logic is abnormal , Or recalculation ( Long calculation ) Under the circumstances , It is best to avoid using tuple conditional expressions .

Decorator

Decorator (Decorators) yes Python An important part of . In short : They are functions that modify the functions of other functions . They help keep our code shorter , And more Pythonic(Python aron ). Most beginners don't know where to use them , So I'm going to share , Which areas of decorators can make your code more concise .

First , Let's discuss how to write your own decorator .

This is probably one of the most difficult concepts to master . We only discuss one step at a time , So you can fully understand it .

Everything is the object

First, let's understand Python The function in

def hi(name="yasoob"):
    return "hi " + name
print(hi())
# output: 'hi yasoob'
# We can even assign a function to a variable , such as
greet = hi
# We are not using parentheses here , Because we're not calling hi function
# It's putting it in greet In variables . Let's try to run this
print(greet())
# output: 'hi yasoob'
# If we delete the old hi function , See what happens !
del hi
print(hi())
#outputs: NameError
print(greet())
#outputs: 'hi yasoob'

Define a function in a function

Those are the basics of functions just now . Let's take your knowledge further . stay Python In, we can define another function in one function :

def hi(name="yasoob"):
    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")
hi()
#output: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
# It shows that whenever you call hi(), greet() and welcome() Will be called at the same time .
# then greet() and welcome() Function in hi() Cannot be accessed outside the function , such as :
greet()
#outputs: NameError: name 'greet' is not defined

Now we know that we can define another function in the function . in other words : We can create nested functions .

Now you need to learn more , Even functions can return functions .


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