程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python basics D7

編輯:Python

1. Nested function ( Internal function )

Under what circumstances do you usually use nested functions ?

1). encapsulation - Data hiding cannot be accessed externally “ Nested function ”.

2). implement DRY(Don’tRepeatYourself) Principle nested function , Let's avoid duplicate code inside the function .

3). Closure

2.nonlocal keyword

nonlocal Local variables used to declare the outer layer .global Used to declare global variables .

3.LEGB The rules

Python Looking for “ name ” when , Is in accordance with the LEGB Rule lookup :Local-->Enclosed-->Global-->Builtin

Local It refers to the method interior of a function or class

Enclosed Refers to nested functions ( One function encapsulates another , Closure )

Global It refers to the global variables in the module

Built in refer to Python A special name reserved for yourself .

4. object-oriented

notes :Python Support process oriented 、 object-oriented 、 Functional programming and other programming paradigms .

4.1 object

Put different types of data 、 Method ( It's a function ) Put it together , It's the object

4.2 The definition of a class

We define the properties of data types through classes ( data ) And methods ( Behavior ), in other words ,“ Class packages behavior and state together ”.

An object is a concrete entity of a class , Commonly referred to as “ Class ”. Class as “ Cookie Mold ”, The object is based on this “ The mould ” Made “ cookies ”.

Python in ,“ Everything is the object ”. Class is also called “ Class object ”, An instance of a class is also called “ Instance object ”.

4.2.1 The syntax format of the definition class is as follows :

class Class name :

The class body

4.2.2 

The point is as follows :

1. Class name must match “ identifier ” The rules of ; General rules , title case , Use more than one word “ Hump principle ”.

2. In the class body, we can define properties and methods .

3. Attributes are used to describe data , Method ( It's a function ) Used to describe operations related to these data .

4.2.3 _init__ Construction method and __new__ Method

Classes are abstract , Also known as “ Templates for objects ”. We need to use this template through the class , Create instance object of class , Then you can use the functions defined by the class .

individual Python The object contains the following parts :

1.id(identity Identification code )

2.type( object type )

3.value( The value of the object )

         (1) attribute (attribute)

         (2) Method (method)

4.3  Instance properties and instance methods  

4.3.1  Instance attributes   

Instance properties are properties that are subordinate to instance objects , Also known as “ Instance variables ”

4.3.2  Example method

Instance methods are methods that are subordinate to instance objects . The definition format of the instance method is as follows :

def    Method name (self[, Parameter list ]):

      Function body method

The call format is as follows :

object . Method name ([ Argument list ])

4.3.3  The difference between function and method

1. They are all statement blocks used to complete a function , The essence is the same .

2. Method call , Call through object . Method is subordinate to a specific instance object , Ordinary functions do not have this feature .

3. Intuitively , Method definition needs to pass self, The function doesn't need .

4.3.4  Class object 、 Class properties 、 Class method 、 Static methods

5  Memory analysis instance object and class object creation process

6._del__ Method ( Destructor ) And garbage collection mechanisms

_del__ The method is called “ destructor ”, Used to implement the operations required when the object is destroyed .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved