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

Pyhon Learning Notes 3: modules (referencing third-party modules)

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

Share

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

Modules are more commonly called class libraries or modules. In actual development, standard modules or third-party modules of the system will be used. For example, time is related to the time block of the system, file operation is related to the os module, and web automated testing using selenium uses the selenium extension block.

Reference module:

Import...

From.. Import...

For example: output system time

Import time

Print (time.ctime ())

Results:

= = RESTART: D:/selenium/test/5.py =

Thu Nov 3 15:08:13 2016

> > >

If you decide to introduce only the ctime method under the time module, you can use from time import ctime directly

From time import *

Print (ctime ())

Sleep (2)

Print (ctime ())

Output result:

= = RESTART: D:/selenium/test/5.py =

Thu Nov 3 15:12:57 2016

Thu Nov 3 15:12:59 2016

> > >

2 calls between modules

Create a new module and create two files pub.py Huawei count.py under this module

File pub.py:

Def add (aformab):

Return aquib

File count.py:

From pub import add

Print (add (4. 5))

Print the results:

= RESTART: D:/selenium/test/count.py =

nine

> > >

3. Cross-directory module call

File module directory:

Project/

|-- model/

|-- pub.py

|-- count.py

Cout.py Code:

From model.pub import add

Print (add (4. 5))

4. Further discuss cross-directory calls

File directory:

Project/

|-- model/

|-- count.py

|-- new_count.py

|-- test.py

Count.py file code:

Class A ():

Def add (self,a,b):

Return aquib

New_count.py file code

From count import A

Class B (A):

Def sub (self,a,b):

Return a Murb

St = B (). Add (2)

Print (st)

Test.py file code:

Import sys

Sys.path.append (". / model")

Fome model import new_count

Test = new_count.B ()

Test.add (2 and 5)

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

Internet Technology

Wechat

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

12
Report