Python Programming Tips 2

Source: Internet
Author: User

Modular----This is what our programmers dream of, and by modularity we can avoid repetitive manufacturing wheels.

While

Modules allow you to logically organize your Python code snippets.

Assigning the relevant code to a module will make your code better and easier to understand.

The module is also a Python object, with a random name attribute used to bind or reference.

Simply put, the module is a file that holds the Python code. Modules can define functions, classes, and variables. The module can also contain executable code.

For example, print output:

Encapsulates the printed data as a module, and each time the module is called, the text that begins with Hello is output

Module code:

Vim demomodule1.py

The contents are as follows:

#!/usr/bin/python
#-*-Coding:utf-8-*-
# define module: Begin:
def sayHello (hello):
Print "Hello,", hello
Return # #结束定义 End


The function of using the module is as follows:

#!/usr/bin/python
#-*-Coding:utf-8-*-
# #导入模块 functions defined in the demode.py
Import DemoModule1

# #call function in demodef.py
Demomodule1.sayhello ("haha")
# # #这种调用的方法是错误的 SayHello ("++++++++1234567++++")

Run and output:

Error Summary:

This is forgotten add:

Python Programming Tips 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.