Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the writing methods of python printing?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article Xiaobian for you to introduce in detail "what are the writing methods of python printing", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "what is the writing method of python printing" can help you solve your doubts? the following is followed by Xiaobian's train of thought to learn new knowledge.

I. preliminary preparation

Suppose the code in parent/__init__.py is:

#-*-coding: UTF-8-*-print ('there used to be a mountain,')

Suppose the code in parent/one/__init__.py is:

#-*-coding: UTF-8-*-print ('there is a temple on the hill,')

Suppose the code in parent/one/one/__init__.py is:

#-*-coding: UTF-8-*-print ('there is an old monk in the temple')

Suppose the code in parent/two/__init__.py is:

#-*-coding: UTF-8-*-print ('old monk said:')

Suppose the code in parent/three/__init__.py is:

#-*-coding: UTF-8-*-print ('there used to be a mountain,')

Create the py file for as shown above. The corresponding effect is shown in the following figure. It feels like a wireless doll, which is very interesting, .

2. Multiple import calling methods

If a module has been import, Python will cache the imported module in the sys.modules dictionary. When it is imported again, it will not re-perform the import action, but will be fetched directly from the cache. On the other hand, if we delete the imported module from sys.modules, import will trigger the module import action again.

Using the above knowledge, our goal is to import the module through the import statement and trigger the print statement to be executed when the _ _ init__.py is loaded in each package directory, thus printing out:

There used to be a mountain

There is a temple on the hill

There is an old monk in the temple,

The old monk said:

There used to be a mountain

There is a temple on the hill

There is an old monk in the temple,

The old monk said:

There used to be a mountain

...

First, we introduce the use of del in python. Del is used to remove references to the corresponding variables.

Because python are references, and python has a GC mechanism, del statements act on variables, not data objects.

If _ _ name__=='__main__': aquifer 1 # object 1 is referenced by variable a, object 1 is referenced by variable b, object 1 is referenced by variable b, object 1 is referenced by variable c, object 1 is referenced by variable c, reference counter of object 1 plus 1 del a # deletes variable a Dereferencing a to 1 del b # removing variable b, dereferencing b to 1 print (c) # final variable c still refers to 1

Del deletes variables, not data.

1. The first way to write import sys if _ _ name__ ='_ _ main__': while True: import parent.one.one import parent.two import parent del sys.modules ['parent.one'] del sys.modules [' parent.one.one'] del sys.modules ['parent.two'] del sys.modules [' parent']

Effect picture

2. The second way to write import sys if _ _ name__ ='_ _ main__': while True: import parent.one.one import parent.two del sys.modules ['parent'] del sys.modules [' parent.one'] del sys.modules ['parent.one.one'] del sys.modules [' parent.two']

Effect picture

3. The third way to write import sys if _ _ name__ ='_ _ main__': while True: import parent import parent.one import parent.one.one import parent.two del sys.modules ['parent'] del sys.modules [' parent.one'] del sys.modules ['parent.one.one'] del sys.modules [' parent.two']

Effect picture

After reading this, the article "what are the ways to print python?" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, you are welcome to follow the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report