python learning the basics of three notes of objects

Object Basics

Objects

  • python is an object-oriented language, everything is an object in python;
  • When the program is run, the data are loaded into memory and then run;
  • The object is a container, is an area designated for storing data memory;

Structure of objects

Each object has three data python
Here Insert Picture Description

id (identification)
  • id used to distinguish objects, id number of each object is different from each other;
  • Id function to view the object through the id ();
    Here Insert Picture Description
  • python id generated by the parser, in the CPython, id is the object memory address;
  • After the object is created, id change does not occur;
type (type)
  • Type is the type of data object, such as int, bool, str, float;
  • You can see the type of object type function ();
  • Different types of objects having different functions;
  • python is a strongly typed language, created after the object type can not be changed;
value
  • Value is the data stored in the object;
  • Subjects were divided into immutable objects and variable objects, object value can be a variable constant, immutable objects immutable values, generally objects are immutable;

Variables and objects

  • python like objects from the object to the alias;
  • No variable value stored object, but stored object id, i.e., objects in memory address;
  • a=12,b=‘hello’
    Here Insert Picture Description
  • When using a variable, that is, viewing the corresponding object by the object ID;
  • Save the variable objects is changed after re-assignment;
    Here Insert Picture Description
  • Are independent variables, modify a variable does not affect the other
    Here Insert Picture Description

Type Conversion

  • The type of conversion is one type of object into another type of the object;
  • Type conversion does not change the type of the object itself, but to create a new object before the object is the value assigned to it;
    Here Insert Picture Description
The type of conversion function

Type conversion functions will not affect the original object, but after converting the type of the object to the specified value returned as the return value;
int ()

  • Action: convert other types of objects to an int;

  • For strings, only when the string is transferred to an integer, the remaining character string can not be converted;
    Here Insert Picture Description
    Here Insert Picture Description

  • For floating point rounding down directly;
    Here Insert Picture Description

  • For bool type True, False, int (True) = 1, int (False) = 0;

float()

  • Action: The other types of objects to a float;
  • For strings, only when the string is integer, float to turn, rest of the string can not be converted;
    Here Insert Picture Description
    Here Insert Picture Description
    Here Insert Picture Description
  • For the number of type int, can be added directly to 0.01, such as 12 becomes 12.0;
  • For bool type True, False, float (True) = 1.0, int (False) = 0.0;

str()

  • For any other type can be directly converted to a string
    Here Insert Picture Description
    BOOL ()
  • Action: convert to other objects bool value;
  • All objects are represented by an empty conversion to False, as in int 0, float in 0.0, str of "", the remaining conversion is True; if
    Here Insert Picture Description
    Here Insert Picture Description
Published 38 original articles · won praise 21 · views 2344

Guess you like

Origin blog.csdn.net/Late_whale/article/details/104058956