In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are three seemingly simple Python problems". Friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the three seemingly simple Python problems"?
Question 1
Suppose we have several variables:
X = 1 y = 2 l = [x, y] x + = 5 a = [1] b = [2] s = [a, b] a.append (5)
What is the print result of l and s?
Skip to solution
Question 2
Let's define a simple function:
Def f (x, s=set ()): s.add (x) print (s)
If you decide what will happen:
> > f (7) > > f (6, {4,5}) > > f (2)?
Skip to solution
Question 3
Let's define two simple functions:
Def f (): l = [1] def inner (x): l.append (x) return l return inner def g (): y = 1 def inner (x): y + = x return y return inner
What will be the result of the following command?
> > ff_inner = f () > > print (f_inner (2)) > > gg_inner = g () > > print (g_inner (2))
Skip to solution
How confident are you in your answer? Let's see if you are right.
Solve problem 1
> > print (l) [1,2] > > print (s) [[1,5], [2]]
Why does the second list react to changes to the first element a.append (5), but the first list completely ignores similar changes to x + = 5?
Solve problem 2
Let's see what happened:
> f (7) {7} > f (6, {4,5}) {4,5,6} > f (2) {2,7}
Wait, isn't the final output {2}?
Solve problem 3
The output will be the following:
> > ff_inner = f () > > print (f_inner (2)) [1,2] > > gg_inner = g () > > print (g_inner (2)) UnboundLocalError: local variable 'y' referenced before assignment
Why doesn't g_inner (2) output 3? How does the internal function of f () remember its outer scope, while the internal function of g () doesn't? They are actually the same!
Description
What if I tell you that these weird behaviors have something to do with the difference between mutable and immutable objects in Python?
Mutable objects such as lists, collections, or dictionaries can be changed (mutated) in place. Immutable objects (such as integers, strings, and tuples) cannot-"changes" to such objects cause new objects to be created.
Description of question 1
X = 1 y = 2 l = [x, y] x + = 5a = [1] b = [2] s = [a, b] a.append (5) > > print (l) [1,2] > > print (s) [[1,5], [2]]
Because x is immutable, the operation x + = 5 does not change the original object, but creates a new object. The first element of the list still points to the original object, so its value remains the same.
For the mutable object a. Append (5) changes the original object, so list s "sees" the change.
Explanation of question 2
Def f (x, s=set ()): s.add (x) print (s) > > f (7) {7} > > f (6, {4,5}) {4,5,6} > > f (2) {2,7}
The first two outputs make perfect sense: first add a value of 7 to the default empty set to get {7}, and then add a value of 6 to a set of {4pm 5} to get {4p 5pm 6}.
But then a strange thing happened: add a value of 2 to the default empty set instead of to the {7} set. Why? The default value of the optional parameter s is evaluated only once-it will be initialized to an empty set only during the first call to s. Because s is variable after calling f (7), it is modified in place. The second call, f (6, {4pm 5}), does not affect the default parameter-the provided collection {4jue 5} obscures it, in other words, {4J 5} is a different variable. The third call f (2) uses the same s variable as the first call, but s is not reinitialized as an empty set-using its previous value {7}.
This is why you should not use variable default parameters. In this case, the functionality should be modified as follows:
Def f (x, s=None): if s is None: s = set () s.add (x) print (s)
Explanation of question 3
Def f (): l = [1] def inner (x): l.append (x) return l return inner def g (): y = 1 def inner (x): y + = x return y return inner > > ff_inner = f () > > print (f_inner (2)) [1,2] > > gg_inner = g () > > print (g_inner (2)) UnboundLocalError: local variable 'y'referenced before assignment
In this problem, we deal with closures-internal functions remember the appearance of their closed namespaces when they are defined. Or at least it should be-the second feature keeps the poker face as if it had never heard of its external scope.
Why is that? When we execute l.append (x), the mutable object created at definition time is modified, but the variable l still points to the same address in memory. However, trying to change the immutable variable in the second function, y + = x, will cause y to point to a different address in memory than before-the original y will no longer be remembered, resulting in UnboundLocalError.
At this point, I believe you have a deeper understanding of "what are the three seemingly simple Python problems?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.