In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to understand Python built-in objects". In daily operation, I believe many people have doubts about how to understand Python built-in objects. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "how to understand Python built-in objects"! Next, please follow the editor to study!
The so-called special way of writing is the following:
# use. Replace pass def foo ():...
It is half an ellipsis of Chinese punctuation, that is, it consists of three dots in English. If this is the first time you've seen it, you'll probably wonder: what's going on with this thing? PS: if you know it, after reading this article carefully, you may also find it strange!
1. Get to know "." Built-in constant
In fact, it is a built-in object in Python 3 with an official name-Ellipsis, which translates into Chinese as "ellipsis".
More precisely, it is a built-in constant (Built-in Constant), one of the six built-in constants (the others are None, False, True, NotImplemented, _ _ debug__).
Here is a screenshot of the basic nature of this object, and you should know what I mean:
"..." It's not mysterious, it's just a symbolic object that may be rare. Replacing pass with it does not report an error in syntax, because Python allows an object to be referenced without an assignment.
Strictly speaking, this is an outsider and semantically untenable. Or other constants or variables that have been assigned are placed in an empty indented block that has nothing to do with the action and can only express "there is a useless object, ignore it."
Python allows these objects that are not actually used to exist, but smart IDE should give a hint (I'm using Pycharm), such as telling you: Statement seems to have no effect.
But "." This constant seems to have been given special treatment, and there is no hint on my IDE.
Many people are used to using it as a null operation like pass (an example of this usage was given in the mail group discussion that first introduced it). But I still prefer to use pass. I don't know what you think.
two。 Strange Ellipsis and...
... Introduced in PEP-3100, it was first incorporated into Python 3.0, while Ellipsis was included in earlier versions.
Although officially they are written in two ways of the same object and described as singleton, I also found a very strange phenomenon that conflicts with the description of the document:
As you can see, assign a value to. There will be an error SyntaxError: cannot assign to Ellipsis, but Ellipsis can be assigned, and their behavior is fundamentally different! After being assigned, the memory address and type properties of Ellipsis are changed, and it becomes a "variable" and is no longer a constant.
In contrast, when assigning a constant such as True or None, you get an error SyntaxError: cannot assign to XXX, but you don't get an error when assigning a constant to NotImplemented.
It is well known that Boolean objects (True/False) can also be assigned in Python 2, but Python 3 has transformed them into immutable.
So there is a possible explanation: Ellipsis and NotImplemented are legacies of the Python 2 era and can be assigned and modified in the current version for compatibility or simply because the core developers have omitted them.
... Born in the age of Python 3, it may completely replace Ellipsis in the future. At present, the two coexist, and their inconsistent behavior is worthy of our attention. My advice: use only "." Let's just say Ellipsis has been eliminated.
3. Why use "..." A date?
Next, let's go back to the question of the title: why does Python use "..." A date?
Here we only focus on the "..." of Python 3. Not to trace the history and current situation of Ellipsis.
The reason why I asked this question, my intention is to know: what is the use of it, what problems can be solved? As a result, we can peep into more details of Python language design.
There are probably the following answers:
(1) extended slicing syntax
The official documentation gives the following instructions:
Special value used mostly in conjunction with extended slicing syntax for user-defined container data types.
This is a special value, usually combined with extended slicing syntax and used on custom data type containers.
No specific implementation example is given in the document, but it can be used to combine the built-in functions of _ _ getitem__ () and slice () to achieve the effect of taking out 7-digit slices similar to [1,..., 7].
Because it is mainly used in data manipulation, most people may have little contact with it. I heard that Numpy uses it in some grammatical sugar usages. If you are using Numpy, you can explore what kinds of ways to play.
(2) expressing the semantics of "unfinished code"
... Can be used as a placeholder, that is, I mentioned the role of pass in "Why does Python have a pass statement?" There has been some analysis on this in the previous article.
Some people think this is cute, and this idea is supported by Guido, the father of Python:
(3) Type Hint usage
The Type Hint introduced by Python 3.5 is "..." The main occasions for use.
It can represent parameters of variable length, such as Tuple [int,...] Represents a tuple whose elements are of type int, but the number is unlimited.
It can also represent uncertain variable types, such as the example given in the document:
From typing import TypeVar, Generic T = TypeVar ('T') def fun_1 (x: t)-> T:... # T here def fun_2 (x: t)-> T:... # and here could be different fun_1 (1) # This is OK, T is inferred to be int fun_2 ('a') # This is also OK, now T is str
T cannot be determined when the function is defined, and the actual type of T is determined only when the function is called.
In a file in .pyi format,. It can be seen everywhere. This is a kind of stub file (stub file), which is mainly used to store the type prompt information of the Python module and to do static code checking to type checking tools such as mypy, pytype and IDE.
(4) represents an infinite loop
Finally, I think there is a very ultimate reason, except for the introduction of "." To show that there is no better way.
Let's take a look at two examples:
"..." appears in the results of both examples. What does it mean?
For containers such as lists and dictionaries, references to mutable objects are stored if their internal elements are mutable objects. Then, when its internal elements refer to the container itself, an infinite circular reference recursively occurs.
Infinite loops are inexhaustible and are used in Python. To show that the image is easy to understand, apart from it, I am afraid there is no better choice.
Finally, let's summarize the content of this article:
Is a built-in constant in Python 3. It is a singleton object. Although it is a nickname for Ellipsis in Python 2, its nature has parted ways with the old object.
You can replace the pass statement as a placeholder, but as a constant object, it is not rigorous in terms of placeholder semantics. Many people are used to accepting it, so you might as well use it.
In addition to placeholder usage, many usage scenarios in Python can also support extended slice syntax, rich Type Hint type checking, and infinite loops that represent container objects.
For most people, it may be rare (some people may reject it because it is a symbolic exception), but its existence can sometimes bring convenience. I hope this article can let more people know it, then the purpose of the article will be achieved.
At this point, the study on "how to understand Python built-in objects" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.