Catalog
Ternary operator
Decorator
Everything is the object
Define a function in a function
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_falseExample :
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 fatThis 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 (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 .
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'
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 definedNow 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 .
Student address book management system 3.0 of Python basic project practice
Address book management system
Learn pyside2 to develop artificial intelligence system application based on Python
Study PySide2 be based on Pyth