Everything in Python objects (Study Notes 1)

Foreword

    Python former self when many of them are probably glance, advanced course have seen some superficial but are skipped because at that time also just started muddle. Now use some of the time frame, only know Yihuhuhuapiao not know these views are now ready to look at themselves in high-level programming Python, writing a blog to share to share with you.

Outline

This paper consists of four parts:

everything is an object in python

type, object and class relationships

Common types of built-in python

to sum up

everything is an object in python

Functions and classes are objects, first-class citizens belong to the python
So what is the first-class citizens:

  1. Assigned to a variable
  2. You can add to the collection object
  3. It can be passed as a parameter to the function
  4. It can be used as the return value of the function

1. assigned to a variable

Example 1:

def ask(name="hello"):
    print(name)

# 函数赋值给一个变量
my_func = ask
my_func('hello')

result:

hello

Example 2:

class Person:
    def __init__(self):
        print('hello2')

# 类赋值给一个变量
my_class = Person
my_class()

result:

hello2

Examples illustrate the functions and classes are objects

2. collection object can be added to

def ask(name="hello"):
    print(name)


class Person:
    def __init__(self):
        print('hello2')


object_list = []
object_list.append(ask)
object_list.append(Person)

for item in object_list:
    print(item())

result:

hello  # 调用ask 输出
None  # ask没有return所以为 None
hello2  # 调用 Person
<__main__.Person object at 0x000001E57B8A2688>  # Pseron 实例化返回一个类对象

3 can be passed as a parameter to the function

object_list = []
object_list.append(ask)
object_list.append(Person)


def print_type(item):
    print(type(item))

for item in obj_list:
    print_type(item)

result:

<class 'function'>
<class 'type'>

4 can be used as the return value of the function

A function can return another function
decorator implementation principle

def decorator_func():
    print('dec start')
    return ask


my_ask = decorator_func()
my_ask('tom')

result:

dec start  # decorator_func打印
tom  # 根据传递参数由 ask返回

type, object and class relationships

This part is everything is in order to further understand the python objects
Type and Object

  • type vertex objects, all objects are created from type.

  • object class inherits the apex, all classes inherit from object.

  • Everything in python objects, a python object may have two attributes, class and Base , class represents the object who created it, Base indicate who the father of a class yes.

>>> object.__class__
<class 'type'>
>>> type.__base__
<class 'object'>
>>>

The above examples drawn:

  • type class inherits from object
  • object type of object is created from

The following is a more detailed description:

a=1
b="abc"
print(type(1))
print(type(int))


class Student:
    pass

stu = Student()
print(type(stu))
print(type(Student))
print(int.__bases__)
print(str.__bases__)
print(Student.__bases__)
print(type.__bases__)
print(object.__bases__)
print(type(object))
print(type(type))

result:

<class 'int'>					# 1是由int这个类创建的实例
<class 'type'>					# int这个类是由type这个类创建的实例
<class '__main__.Student'>		# int是由type这个类创建的实例
<class 'type'>					# stu是类Student创建的实例
(<class 'object'>,)				# 类int的基类是object这个类
(<class 'object'>,)				# 同上
(<class 'object'>,)				# 同上
(<class 'object'>,)				# 重点:类type的基类也是object这个基类
()								# 重点:类object没有基类
<class 'type'>					# 难点:类object是由类type创建的实例
<class 'type'>					# 难点:类type是由type类自身创建的实例

type->int->1
type->class->obj

Examples can be drawn from the generating type int, int. 1 generates a
type generate classes, class generation obj
Object classes inherit all the top-level base class
type may generate a class or type of the returned object
class generated by the type of the object
type is a class, but also an object
relation chart

  • object of all objects: list, str, dict, tuple base class, while the object is an instance of type
  • Class type is an instance of itself, but also inherited from type object class
  • Conclusions from the conclusions 1 and 2,得出一切皆对象,同时一切皆继承自object类

Common types of built-in python

  • 对象的三个特征: 身份、类型、值
  • None (only one globally)
  • Value: int, float, complex (complex), bool
  • Iterator type
  • Sequence Type: list, {bytes, bytearray, memoryview (binary sequence)}, range, tuple, str, array
  • Mapping (dict)
  • Collection: set, frozenset
  • Context Management type (with)
  • Other (Learn): module type: class and instance, the function type, type method, type of code, object objects, type type, ellipsis type, type NotImplemented
>>> a=1
>>> id(a)
140712501092752			# 内存地址=身份
>>>

身份就是对象在内存中的地址
对象都是有类型的
值就是例子中的1

to sum up

Purpose of this paper is to understand everything in Python are subject
to pave the way for the following update ~~~~~

Published 29 original articles · won praise 19 · views 1327

Guess you like

Origin blog.csdn.net/s1156605343/article/details/104261706