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

How to use statements to import modules or packages in Python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use sentences to import modules or packages in Python, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Import all from one module

From import * means means "I want to be able to access all the names that I have permission to access". For example, the following code something.py:

# something.py

Public_variable = 42

_ private_variable = 141

Def public_function ():

Print ("I'm a public function! yay!")

Def _ private_function ():

Print ("Ain't nobody accessing me from another module...usually")

Class PublicClass (object):

Pass

Class _ WeirdClass (object):

Pass

In the Python interpreter, we can execute from something import * and see something like this:

> from something import *

> public_variable

forty-two

> _ private_variable

...

NameError: name'_ private_variable' is not defined

> > public_function ()

"I'm a public function! yay!"

> _ private_function ()

...

NameError: name'_ private_function' is not defined

C = PublicClass ()

> > c

> > c = _ WeirdClass ()

...

NameError: name'_ WeirdClass' is not defined

From something import * imports all names from something except the names that start with _. According to the specification, the names starting with _ are private, so they are not imported.

Well, it's not so bad! What else?

There is no mention of what _ _ all__ is. All__ is a list of strings that specifies which symbols in the module (or package mentioned later) will be exported when from import * is used. If we don't define _ _ all (we didn't define the something.py above), import * the default import method is to import all names except those beginning with an underscore (_). Again, the programming convention underlining indicates that a symbol is private and that it is reasonable not to import it. Let's see what happens when we define our own _ _ all__ in something.py.

# something.py

_ _ all__ = ['_ private_variable', 'PublicClass']

# The rest is the same as before

Public_variable = 42

_ private_variable = 141

Def public_function ():

Print ("I'm a public function! yay!")

Def _ private_function ():

Print ("Ain't nobody accessing me from another module...usually")

Class PublicClass (object):

Pass

Class _ WeirdClass (object):

Pass Zhengzhou artificial abortion surgery http://rl.zyfuke.com/

Now, we expect from something import * to import only _ private_variable and PublicClass:

> from something import *

> public_variable

forty-two

> _ private_variable

...

NameError: name'_ private_variable' is not defined

> > public_function ()

"I'm a public function! yay!"

> _ private_function ()

...

NameError: name'_ private_function' is not defined

C = PublicClass ()

> > c

> > c = _ WeirdClass ()

...

NameError: name'_ WeirdClass' is not defined

What does the bag look like?

When importing all from a package, _ _ all__ behaves in much the same way as modules, except that it deals with modules in the package (rather than importing all the names in the module). So when we use from import *. Time _ _ all__ describes all modules that need to be imported into the current namespace.

The difference is that if you do not declare in the _ _ init__.py of a package that the _ _ all__,from import * statement will not import anything (this statement is not entirely true, the correct statement is here)

But what's wrong with that?

Write here, recommend a full-resource python learning gathering place, click to enter, here are senior programmers to share previous learning

Experience, study notes, as well as the work experience of front-line enterprises, and carefully collate a python zero foundation to the project actual combat information

Every day, I will explain to you the latest technology and prospects of python, and the small details that you need to leave a message.

From import * is ambiguous. It doesn't tell us what we're importing or what we're bringing into the current namespace. It is better to explicitly import all the names we need. In this way, the reader (probably yourself in the future) won't be confused about where a variable / method / class / other thing used in your code comes from, which tells us the next point:

Readability is important.

Even if you need to import a lot of things, it is clearer to import explicitly one by one. Using PEP 328:

From Tkinter import (Tk, Frame, Button, Entry, Canvas, Text

LEFT, DISABLED, NORMAL, RIDGE, END)

You can know exactly what's in your namespace right now, and using ctrl+f can quickly tell you where they came from.

At the same time, you always have to take the risk that the module / package author changes the list content (plus / minus something). This is one of the following two:

The author deleted a string from _ _ all__. If your code uses that name, your code will report an error in NameError, and it will be difficult to find out why.

The author adds a lot of things to _ _ all__. You may not need these additions, so you just let the things you don't care about fill your namespace. They will even replace other content of the same name when you are not paying attention.

The above is all the contents of the article "how to use statements to import modules or packages in Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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