Introduction to module usage and object orientation in Python

Source: Internet
Author: User

1. Module Use 


Module:

    • Modular Pyth1.on The basic way of organizing code

    • A python script can run on its own, or it can be imported into another script, which we call Modules (module) when the script is imported and run.

    • All the points P for the file can be imported as a module

    • The module name is the same as the file name of the script, for example, we have written a script named HELLO.PV and can import it in another script with import hello.py


Package:

Python modules can be organized into packages by directory

Steps to create a package:

Create a directory with the name of the package name, create a __init__.py file under that directory, and place the script file or compiled extensions and sub-packages in the directory as needed

Grammar:

Import pack.m1,pack.m2,pack.m3


Sys.path

Example:

In [1]: import sys

In [2]: sys.path

OUT[2]:

['',

'/usr/bin ',

'/usr/lib64/python27.zip ',

'/usr/lib64/python2.7 ',

'/usr/lib64/python2.7/plat-linux2 ',

'/usr/lib64/python2.7/lib-tk ',

'/usr/lib64/python2.7/lib-old ',

'/usr/lib64/python2.7/lib-dynload ',

'/usr/lib64/python2.7/site-packages ',

'/usr/lib64/python2.7/site-packages/gtk-2.0 ',

'/usr/lib/python2.7/site-packages ',

'/usr/lib/python2.7/site-packages/ipython/extensions ',

'/root/.ipython ']


VIM/ROOT/.BASHRC

Finally add an export pythonpath=/root/library


Successfully added to the Python environment variable

In [1]: import sys

In [2]: sys.path

OUT[2]:

['',

'/usr/bin ',

'/root/library ',

'/usr/lib64/python27.zip ',

'/usr/lib64/python2.7 ',

'/usr/lib64/python2.7/plat-linux2 ',

'/usr/lib64/python2.7/lib-tk ',

'/usr/lib64/python2.7/lib-old ',

'/usr/lib64/python2.7/lib-dynload ',

'/usr/lib64/python2.7/site-packages ',

'/usr/lib64/python2.7/site-packages/gtk-2.0 ',

'/usr/lib/python2.7/site-packages ',

'/usr/lib/python2.7/site-packages/ipython/extensions ',

'/root/.ipython ']


Scripts that can be called by other scripts:

#!/usr/bin/env python

#

def wordCount (s):

chars = Len (s)

words = Len (S.split ())

lines = s.count (' \ n ')

Print Lines,words,chars

if __name__== ' __main__ ': #设置后, the functions within this script can be called by other scripts test.py

s = open ('/etc/passwd '). Read ()

WordCount (s)

#


[Email protected] 20180109]# python words.py

27 52 1324

[Email protected] 20180109]# wc/etc/passwd

1324/etc/passwd


import words.py in test.py:

[Email protected] 20180109]# cat test.py

#!/usr/bin/env python

Import words

#

s= "" "Hello World Python" "

Words.wordcount (s)

[email protected] 20180109]# python test.py

27 52 1324

[[email protected] 20180109]# ls

test.py words.py words.pyc # # #调用words后自动生成编译的文件.


Modules and Packages:

A module is a script file that can be imported

Packages are directory-organized modules and sub-packages, with __init__.py files in the directory, which can store information about the package.

Syntax for importing modules and packages:

Import,import as

From ... import ...



From the individual name:


2. Object-Oriented Introduction

Classes and objects: Two important concepts in object-oriented

Class: Is an object's abstraction of things, such as human \ Ball

Object: Is an instance of a class, such as football \ Basketball

Example Description:

Balls can abstract the characteristics and behavior of the ball and then instantiate a real sphere.

Why object-oriented?

The main idea of object-oriented is

      • Packaging

      • Inherited

      • Polymorphic

This idea solves more complex projects and is easier to maintain.

Python class definition

Class definition:

Classes combine the required variables and functions, which are called "encapsulation",

Class A (object):

Structure of the class:

Class name

Member Variables-Properties

member functions-Methods


The creation of a class

Class MyClass (object):

def fun (self):

Print ("I am Function")

There is at least one parameter in the class's method self


Examples of class scripts:

class People (object):

color = ' Yellow '

def think (self):

Self.color = "BLACK"

print "I am a%s"% Self.color

print ("I am a Thinker")

ren = people ()

Print Ren.color

Ren.think ()


Introduction to module usage and object orientation in Python

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.