python-Example 3

Source: Internet
Author: User

1. Query module: According to the directory to find the module to be imported, module directory generally in:/usr/lib64/python2.7

In [2]: sys.pathout[2]:[', '/usr/bin ', '/usr/lib64/python2.7/site-packages/mysql_python-1.2.5-py2.7-linux-x86_64. Egg ', '/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/lib/python2.7/site-packages ', '/usr/lib/python2.7/site-packages/python_ Memcached-1.58-py2.7.egg ', '/usr/lib/python2.7/site-packages/ipython/extensions ', '/root/.ipython ']


2. Customizing the module Catalog

Method One:sys.path.append (), usually added at the end of the directory list

In [3]: Sys.path.append ("/root/python/") in [4]: sys.pathout[4]:[', '/usr/bin ', '/usr/lib64/python2.7/site-packages/ Mysql_python-1.2.5-py2.7-linux-x86_64.egg ', '/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/lib/python2.7/site-packages ', '/usr/lib/python2.7/ Site-packages/python_memcached-1.58-py2.7.egg ', '/usr/lib/python2.7/site-packages/ipython/extensions ', '/root/. Ipython ', '/root/python/']


Method Two: Modify the environment variables, usually added in front of the directory list

VIM/ROOT/.BASHRC # Join Export PYTHONPATH=/ROOT/PYTHONSOURCE/ROOT/.BASHRC # Refresh


Example: Count a file, number of lines, number of words, number of characters (same effect as WC command)

Note: To avoid using split cut, the last extra empty string is used, and the count ()

#/usr/bin/env pythondef count (s): char = Len (s) words = Len (s.split ()) lines = S.count ("\ n") Print Lines,words,charfile1 = Op En ("/etc/passwd", "r") s = File1.read () count (s)


3. Script form, import module, script name cannot be number, will produce a compiled file

Example:

#!/usr/bin/env Pythonimport WC

Description: Production compile file under directory: WC.PYC



4.py and wc.py __name__ built-in variables are not the same, the former is WC, or __main__, modify the wc.py, execute their own when the output of their own results, is called when the execution does not show the source results:

wc.py:

#/usr/bin/env pythondef count (s): char = Len (s) words = Len (s.split ()) lines = S.count ("\ n") print Lines,words,charif __name __ = = "__main__": file1 = Open ("/etc/passwd", "r") s = File1.read () count (s)

test.py:

#!/usr/bin/env pythonimport wcs = open ("/root/python/10.py", "R"). Read () Wc.count (s)


5. Package form, import module

Four ways to import: Create a __init__.py empty file in the package directory dir

Method One:

From dir import wcwc.count ("abc")


Method Two:

Import Dir.wcdir.wc.count ("abc")


Method Three:

From DIR.WC import Countcount ("abc")


Method Four: Aliases

From DIR.WC import count as Countcount ("abc")


6. Object-oriented Programming: Python, Java, C + +; process-oriented programming: C, functional programming, Shell

Class (Static) properties: (The human senses, understood as variables)

Class (Dynamic) method: (Human clothing and shelter, understood as a function)

Object: The instantiation of a class before it can have properties and methods



7. Creation of classes

Class, there is at least one argument to the self

When calling properties, without parentheses

When calling a method, use parentheses; When a method invokes a property, there is at least one self argument

Property calls other methods: Class name. property name


Example:

Class people (): color = "Yellow" Def think (self): Self.color = "BLACK" Print ("My color is%s"% (self.color)) ren = people () # class instantiation of the print Ren.color # class is called outside of the properties of the Ren.think () # class, and a default return value of None is used, such as add print

Operation Result:

Yellow

My Color is black



8. Private properties are called in an intrinsic function in a defined class

Example:

Class people (): color = "yellow" __age = 27def Think (self): Self.color = "Black" Print Self.__age # Internal The function calls the private property of the class, and the external function cannot call print directly ("My color is%s"% (self.color)) ren = people () print ren.colorren.think ()


9. External call private Property (format: instance alias. _ Class Name Property name ), usually just for testing

Example:

Class people (): color = "yellow" __age = 27def Think (self): Self.color = "BLACK" Print self.__ageprint ("My color is%s"% (SE Lf.color)) ren = people () print ren.colorren.think () print Ren._people__age # External Call private property


10. Methods of the class

Public methods: both internal and external can be called

Private method: Internal function call

Dynamic method: Classmethod () function processing, other parameters of the class that are not called are not loaded into memory

static method:


The definition of a method is the same as a function, but the self is required as the first argument, and if there are other arguments, continue to add; After the class is instantiated, it takes the class name. Method Name () call


Example 1: Private method invocation

Class people (): color = "yellow" __age = 27def __think (self): Self.color = "BLACK" Print self.__ageprint ("My color is%s"% ( Self.color)) def test (self): Private method Call of the Self.__think () # Class for Private method Calls ren = people () ren.test () # class



Example 2: Dynamic method invocation

Class people (): color = "yellow" __age = 27def __think (self): Self.color = "BLACK" Print self.__ageprint ("My color is%s"% (                  Self.color)) def test (self):p rint ("testing ...") cm = Classmethod (test) # dynamic method Definition ren = people () ren.cm () # dynamic Method Invocation


Example 3: Static method invocation:

The class function does not take the self parameter, which is handled with the Staticmethod () function (if it is not handled, the self is missing, the call is error), and all the things about this class are loaded

Class people (): color = "yellow" __age = 27def __think (self): Self.color = "BLACK" Print self.__ageprint ("My color is%s"% ( Self.color)) def Test (): # intrinsic function, without selfprint ("testing ...") #print People.color # because there is no self, you cannot call the property of Class cm = Staticmethod (test) # static method Definition ren = people () ren.cm () # static method call


Example 4: Add an adorner that works only on one of the following functions, and you can use the method of the class to invoke the

Class people ():color =  "Yellow" __age = 27def __think (self): self.color =   "BLACK" print self.__ageprint  ("my color is %s "%  (self.color)) @ classmethod                #   Decorator def test (self):               #  with selfprint  ("Testing ...") @staticmethod                 #  decorator Def test1 ():                 #  without selfprint  ("Testing1..") Ren = people () people.test ()              Method call for     #  class People.test1 ()           Method invocation of the       #  class



python-Example 3

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.