Python study notes: For <Python3 study notes> Learning finishing, Chapter 2 type [1]

2.1.1 impression

We are accustomed to life is divided into animals, plants, and then another feline, canine and other segments, through the study of the individual, summarized their common features, to facilitate the description of the race abstract template Once you have templates can be created accordingly similar large number of individual behavior, so the classification is a basic project.

In professional terms, we will take the ethnic or class type (class), will be called individual instances (instance). Held jointly with the type of behavior of individuals and families shared state, and private properties can be saved only examples. Thus, in the memory space layout is the most efficient.

Each instance of the generic type holds a pointer. If necessary, through its indirect access to the target. However, the external logical interface view, any instance is "complete"

Surviving instance of an object has a "unique" id values:

supplement:

id () function: used to obtain the memory address of the object

Syntax: id ([object])

Returns: Returns the object memory address.

Available type return type instance belongs:

To determine whether the instance of a particular type, the function may be used isinstance

supplement:

the isinstance () function: determining whether an object is a type known

语法:isinstance(object, classinfo)

parameter:

object - the object instance.
classinfo - may be directly or indirectly, class name, or basic types tuple composed thereof.

Return Value: If the parameter type of the two types (ClassInfo) True if the object is the same, otherwise False.

isinstance () and type () the difference:
type () does not think that is a subclass of parent class type, without regard to inheritance.
isinstance () will be considered sub-class is a parent class type, consider inheritance.
To determine whether two of the same type recommended isinstance ()

 

Any of its ancestor types are subclasses of the type, the same object can also be determined for instance of the type ancestor

class A:
    pass
class B(A):
    pass
print(issubclass(B,A))

result:

supplement:

issubclass (): the parameter for determining whether the class is a subclass of a type parameter classinfo

语法:issubclass(class, classinfo)

parameter:

class - class
classinfo - class

Return Value: If the class is a subclass of classinfo returns True, otherwise False

Note: all types have a common ancestor type object, which provides the original template for all types, as well as the basic mode of operation of the system required

class A:
    pass
class B(A):
    pass
print(issubclass(B,object))

result:

 

Although the type is an abstract concept of ethnic groups, but also just an ordinary object instance, the difference is in the realization of all types are created by the type, it does not matter and inheritance

Solely on the type of the object, its essence is to use a special container storage method and field member, with the same one involved is to achieve normal thinking, of course, the type of objects that belong to such a special presence creator, by default, they are interpreted by the automatically generated when you first load the same life cycle and process, and only one instance

2.1.2 name

In the normal cognitive, the variable was a specific format having a memory, the memory variable name is an alias, as in the encoding truncated, can not determine the specific location in memory, so the name of the symbol instead of

Statically compiled and interpreted languages ​​dynamic variable names for different points of approach:

Static compilation: static compiler or linker to a fixed address, or direct, indirect addressing instruction instead of the variable name, that is, the name is not involved in the implementation process, it can be eliminated.

Dynamic interpretation of language: the names and variables are typically run two entities. Not only the name has its own type, but also need to allocate memory and perform interventional procedures. We can even say the name of the foundation is the dynamic model

The name must be associated with the target objects together makes sense, the most direct correlation operation is negative, then a reference to the name is interpreted as the target object operation

Assignment steps:

1. Prepare the right value of the target object

2. Prepare a good name

3. association (namespace {name: the target object}) is both the name space

Even so, the name of the target object is only a reference to the associated name is only responsible for hiring, but know nothing about this person. Given the run in order to know the name of the referenced object type, so that Python is a dynamically typed language

Namespace:

Namespace (namespace): it is designed to store the name and destination refer to the associated container

For Python, each module (source file) has a global name space. According to the scope of the code, but also the current name of the control or a local name space that, if executed directly at the module level, then the current name space and the same global name space, but within the function, it specifically refers to the current namespace function scope

Use the default namespace dictionary data structure, a plurality of key-value pairs

Built-in functions globals and locals return the global namespace and local name space dictionary

Code 1:

y = 100
 print (id (Global ()))
 print (id (local ()))
 print (Global ())
 print (local ())

Result 1:

30466768
30466768
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x00000000003A1CC0>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'D:/Pyexerice/func_lambda1.py', '__cached__': None, 'y': 100}
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x00000000003A1CC0>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'D:/Pyexerice/func_lambda1.py', '__cached__': None, 'y': 100}

At this point the same globals and locals, the same memory address, the same results

Code 2:

def test (): 
    y = 100
     print (id (Global ()))
     print (id (local ()))
     print (Global ())
     print (local ()) 
test ()

Results:

30663376
39180904
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x0000000001CF1CC0>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'D:/Pyexerice/func_lambda1.py', '__cached__': None, 'test': <function test at 0x0000000001D2C1E0>}
{'y': 100}

You can see, this time different globals and locals memory address, you can see, globals always points to the module name space, while locals are pointing to the current scope environment

To be continued

Guess you like

Origin www.cnblogs.com/reseelei-despair/p/11127627.html