Python objects

Everything is inPythonIs an object.

Even the values of basic basic types (integer, string, floating point...) are objects. Lists are objects, tuples, dictionaries, and everything.

Object hasAttributeswithmethodIt can be accessed using dot syntax.

For example, try to define a new variable of typeint:

age = 8

ageNow you can access the properties and methods defined for all objectsintObject.

For example, this includes accessing the real and imaginary parts of the number:

print(age.real) # 8
print(age.imag) # 0

print(age.bit_length()) #4

# the bit_length() method returns the number of bits necessary to represent this number in binary notation

Variables with list values can access another set of methods:

items = [1, 2]
items.append(3)
items.pop()

The method depends on the type of value.

Thisid()The global functions provided by Python allow you to check the location of a specific object in memory.

id(age) # 140170065725376

Your memory value will change, I will just give an example

If you assign a different value to a variable, its address will change because the content of the variable has been replaced by another value stored elsewhere in memory:

age = 8

print(id(age)) # 140535918671808

age = 9

print(id(age)) # 140535918671840

However, if you modify the object using the method of the object, the address remains the same:

items = [1, 2]

print(id(items)) # 140093713593920

items.append(3)

print(items) # [1, 2, 3] print(id(items)) # 140093713593920

The address will only change when you reassign the variable to another value.

Some objects areVolatile,some areimmutable. It depends on the object itself. If the object provides a way to change its content, it is mutable. Otherwise, it is immutable. Most types defined by Python are immutable. For example aintIs set in stone. There is no way to change its value. If you use increase value

age = 8
age = age + 1

#or

age += 1

Then you checkid(age)You will findagePoint to another storage location. The original value has not been changed, we switched to another value.


More python tutorials: