Take a function as a parameter
Import mathdef Add (x, Y, f): return f (x) + f (Y) print Add (9, math.sqrt)
Map (f, list) functions
Receives an F and a list and, by acting on each element of the list in turn, the function f, gets a new list and returns.
def f (x): return x *= [1234]print map (f, list) # [1 49]
Reduce (f, list) function
def f (x, y): return x * yreduce (f, [12341*2* 3*4
Filter (f, list) function
Filters for elements that do not meet the criteria, and returns a new list of eligible elements
Sorted (list) Sort functions sorted (list, f) custom sort
Import Module
Import Math from math import pow, sin, log
Dynamic Import Module
Try : import jsonexcept importerror: as JSON
Using __future__
New versions of Python introduce new features, but in fact these features already exist in the previous version. To "try out" a new feature, you can do so by importing some of the features of the __future__ module.
from __future__ import unicode_literals
Python Module management tools
Easy_install
Pip
Create Class
class Person (object): = person ()
To add an attribute to an instance of a class
' Wangxi ' -
Initializing instance Properties
classPerson (Object): Def __init__ (self, name, age, gender): Self.name=name Self.age=Age Self.gender=Genderuser= Person ('Wangxi', -,'male') # User.Name'Wangxi'# User.age -# User.gender'male'
Access restrictions for object properties (private property)
classCar (Object): Def __init__ (self, name, color, price): Self.name=name Self.color=Color Self.__price=Pricetesla= Car ('Tesla',' White','$23000') Tesla.name #'Tesla'Tesla.color #' White'tesla.price# Traceback (most recent): # File"<stdin>", line1,inch<module># Attributeerror:'Car' Objecthas no attribute' Price'
Creating Class properties and methods
Binding on a class, each instance owning, independent of each other
classHuman (Object): Address='Earth'__name='Mans'# Private attribute, inaccessible to Def __init__ (self, name) in an instance of class: Self.name=name def getName (self): # method of the class the first argument must be selfreturnSelf.__name # Class of methods can access private properties me= Human ('Wangxi') Me.address #'Earth'me.getname () # Self does not need an explicit pass-through
Class properties can be added and modified dynamically (all instance access class properties will change)
' China ' 'China'
If instance properties and Class properties conflict, the lookup instance property is preferred
' Hangzhou ' 'Earth'
Inherited
class Person (object): def __init__ (self, Name, gender): = name = gender class Student: def __init__ (self, name, gender, score): super (Student, self). __INIT__ (name, gender): = Score
Polymorphic
Special methods
__str__
Turn an instance of a class into Str
The back of the feeling for me a little more than the outline, temporarily do not write down. Come back to school when you're free
Advanced Python Learning Notes