Getting started with Python-function usage to the advertised installation of the program

Source: Internet
Author: User

Getting started with Python-using the function to advertise the installation of Python

This article is mainly suitable for people with certain programming experience, at least master a programming language to view.

Most of the examples are simple to understand English words can understand the level, mainly speaking of Python's overall use of methods and structure. Not a high-level design, it will help you get started with Python.

Python and Java control. You'll see that Python is designed to be concise, useful, and powerful, and that each program ape is worth learning and mastering.

The definition and usefulness of Python functions

A function in Python is a named block of code. As with Java, you can take 0 or more of these parameters. Main forms such as

def $函数名($參数):    ...    函数体    ...

You can see that python replaces the {} in Java with indented statements, grouping the code together.
such as the basic statement in Python:

forin list:    ...    do something    ...while true:     ...     do something     ...if true:     ...else:     ...

Write a sample that prints a different result by the type of the number of parameters:

###假设是一个列表类型,则循环打印,否则打印当前def print_test(is_list):     if isinstance(is_list, list):           forin is_list:                 print(t)                 print_test("not list")     else:           print(the_list)

The list in Python can understand the list in the Java, and the tuple as an array in Java (enclosed in parentheses) seems more powerful and concise than the data. We can understand that the "chicken blood" data can be easily scaled. The relevant methods are:

Len (list)
List.insert (1, ")
List.remove (")
List.append (")

In the above example, a recursive call is used. A class that has an incoming parameter type recursively calls itself.

Can see. A def modifier was added to the method name before it was written directly.

The Python design philosophy sees everything as an object or a collection. Type does not care about what kind of interior it is. Variable identifiers do not require a type at all. In Java, you must declare a variable to indicate the type. Being able to think of Python as a high-level collection, for a list, can store different types of values, just give you a name. The rest is done by Python.

The Isinstance function in the sample is a python built-in function. Similar to instanceof in Java.

The invocation of the function. After saving method.py, F5 executes, type directly in Shell and idle:

### 句未加‘。’ 和写多行句子import method.pyprint_test(["item1","item2","item3"])
Advertising and installation of Python programs

Modular python code, like Java, can build complex and powerful systems. Block python code into a class library for easy management and easy code reuse and architecture.

Import SYS; Sys.path to see where Python is stored on the computer.

Encapsulate the example function as a module, and then advertise the installation as an example:

    • Create a directory for just-written method files: method
      Put the method.py in there.
    • Create a new file "setup.py"
      In the file for the published metadata, edit for example the following:
# 元数据fromimport setupsetup(       name       ‘CankingApp‘,       version    ‘1.0‘,       py_modules = [‘method‘],       author     ‘CankingApp‘,       ‘[email protected]‘,       url        ‘www.baidu.com‘,       ‘test‘,       )
  • Build advertisement file
    Open terminal type command:

    $python 3 setup.py sdist
    running sdist
    Runni NG Check
    warning:check:missing required meta-data:url
    warning:sdist:manifest template ' manifest.in ' does no T exist (using default file list)
    Warning:sdist:standard file not found:should has one of the README, README.txt
    Writing manifest file ' manifest '
    creating CankingApp-1.0
    Making hard links in CankingApp-1.0 ...
    Hard linking hello.py, CankingApp-1.0
    Hard linking setup.py, CankingApp-1.0
    Creating Tar Archive
    Removing ' CankingApp-1.0 ' (and everything under it)

  • fit to Python local copy
    Command in Terminal:

    $ sudo setup.py install
    /usr/lib/python3.4/distutils/dist.py:260:userwarning:unknown distribution option: ' Descripthin '
    Warnings.warn (msg)
    Running Install
    Running Build
    Running Build_py
    Creating build
    Creating Build/lib
    Copying method.py-Build/lib
    Running Install_lib
    Copying build/lib/method.py-/usr/local/lib/python3.4/dist-packages
    byte-compiling/usr/local/lib/python3.4/dist-packages/method.py to Method.cpython-34.pyc
    Running Install_egg_info

When you are done, you will see that the directory has more build and dist directories and MANIFEST files.

    • Build success, test code

Test directly in Idle:

import methodmethod.print_test(["item1","item2","item3"])

The test function call needs to add method, which is the namespace rule in Python.
All of the code in Python is associated with a namespace, and the code in the main program is associated with the "main" namespace. Our individual code modules naturally create a namespace of their own, with the same name as the code block.

So I need to bring method.print_test.

From method Import Print_test
Print_test ()
can also be used, but assume that this namespace has the same name when the conflict expires, the individual feels that the first kind of better.

A successful print out item identifies the successful installation.

The example source has been uploaded to GitHub, interested students are welcome to Exchange learning.

Getting started with Python-function usage to the advertised installation of the program

Related Article

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.