python中一切皆为对象

1. 前言

  在python中一切皆为对象,变量是对象,函数是对象,类也是对象。对象(object)是指在内存中具有唯一标识符(id)类型(type)和值(value)的实例。换句话说,对象是一个具有属性和方法的实体,这些属性和方法可以被访问和操作。

(1)唯一标识符:其实就是对象在计算机内存中的地址。可使用内置函数id(obj)返回对象的内存地址。
(2)类型:表示对象存储的数据类型,使用内置函数type(obj)返回对象所属类型。
(3)值:表示对象存储的数据信息,也就是对象的值。使用内置函数print(obj)可以直接打印值。

2. 使用代码来解释上面的描述

(1)在python中,基本数据类型主要分为6种:数字、字符串(str)、列表(list)、元组(tuple)、字典(dict)和集合(set),其中数字又分为整数(int)、浮点数(float)、复数(complex)。下面以字符串为例,获取其唯一标识符、类型和值。(字符串"hello world"是字符串类型(str)的对象,也就是说"hello world"是一个对象,而对象是可以被赋值给另外一个变量的,并且它具有唯一标识符、类型和值。)

s = "hello world"
print(f"唯一标识符id = {id(s)}")
print(f"类型type = {type(s)}")
print(f"值value = {s}")

输出结果:
唯一标识符id = 2788433130224
类型type = <class 'str'>
值value = hello world

(2)函数和类也是对象,可以通过id(obj)获取其唯一标识符,通过type(obj)返回其所属的类型,通过print(obj)打印其对应的值。

def say_hello():
    print("hello python")

print("--------函数---------")
print(f"函数的唯一标识符id = {id(say_hello)}")
print(f"函数的类型type = {type(say_hello)}")
print(f"函数的值value = {say_hello}")


class Student:
    pass

print("--------类---------")
print(f"类的唯一标识符id = {id(Student)}")
print(f"类的类型type = {type(Student)}")
print(f"类的值value = {Student}")

输出结果:
--------函数---------
函数的唯一标识符id = 1776187004536
函数的类型type = <class 'function'>
函数的值value = <function say_hello at 0x0000019D8CFED678>
-----------------
类的唯一标识符id = 1776178304984
类的类型type = <class 'type'>
类的值value = <class '__main__.Student'>

由于函数和类是对象,而对象是可以被赋值给另外一个变量的。因此我们可以将函数和类赋值给另外一个变量。

def say_hello():
    print("hello python")

my_func = say_hello  # 将函数赋给变量my_func
my_func() # 通过变量my_func来调用函数say_hello()

class Student:
    def __init__(self) -> None:
        print("初始化对象...")

my_class = Student  # 将类赋给变量my_class
my_class()  # 通过变量my_class来创建Student类的对象

输出结果:
hello python
初始化对象...

由于函数和类是对象,所以可以将函数和类存放到python容器中。python的容器有:列表、元组、字符串、集合和字典,下面以列表为例,将函数和类添加到列表中。

def say_hello():
    print("hello python")

class Student:
    def __init__(self) -> None:
        print("初始化对象...")

obj_list = []
obj_list.append(say_hello)  # 将函数添加到列表中
obj_list.append(Student)  # 将类添加到列表中
print(obj_list)

输出结果:
[<function say_hello at 0x0000019C2FF8DAF8>, <class '__main__.Student'>]

3. type、object、class三者之间的关系

(1)总结。看不懂的话可以参考后续的代码,对了要记住一句话:在python中一切皆为对象,变量是对象,函数是对象,类也是对象。

  • 基本数据类型(像int、float、str、list等等)都是一个个的类(class);
  • type()函数可以查询对象的数据类型;
  • 由于类也是对象,所以可以通过type()函数来查询基本数据类型(像int、float、str、list等等)的类型,即type(int)、type(str)、type(list)等等。通过type()函数查询可知,像int、float、str、list这样的类(class)都是type类型;
  • object类是所有类的父类(或者基类),所有的内置类型(像int、float、str、list等等)、用户自定义的类都直接或间接地继承于object类。object类的父类为空;
  • object类是由元类(metaclass)type创建的,但type类又继承了object类,type元类是由自身type创建的;
  • type类是所有类(像int、str、list、object等等)的类型,即所有类(class)都可由type实例化而来;
  • 总而言之:可以把object和type理解为两个体系的王者。object是站在继承关系顶点的存在,所有的类最后都是继承自object的,object没有父类。object是type类型的,也就是说type是站在类型关系顶端的存在,所有的类型都难逃type类型的掌握,所以object和type自己的类型都是type,type的父类是object。
    上面的一句话来自于博主:Python的type和object之间到底是什么关系?

(2)使用type()函数查询基本数据类型(像int、float、str、list等等)对象的类型。还可以使用内置属性__class__来查询对象的数据类型。

# 在python中,10是整数类型的对象,1.2是浮点类型的对象,字符串"hello"是字符串类型的对象,等等。
print(type(10))  # <class 'int'> 整型
print(type(1.2))  # <class 'float'> 浮点型
print(type(True))  # <class 'bool'> 布尔型
print(type("hello"))  # <class 'str'> 字符串类型
print(type([1,2,3]))  # <class 'list'> 列表类型
print(type((1,2,3)))  # <class 'tuple'> 元组类型

(3)使用type()函数查询基本数据类型(像int、float、str、list等等)的类型。

print(type(int))  # <class 'type'> int类型为type
print(type(str))  # <class 'type'> str类型为type
print(type(list))  # <class 'type'> list类型为type

# 还可以使用内置属性__class__来查询对象的数据类型
print(int.__class__)  # <class 'type'> int类型为type
print(str.__class__)  # <class 'type'> str类型为type
print(list.__class__)  # <class 'type'> list类型为type

(4)使用type()函数查询type类和object类的类型。

print(type(type))  # <class 'type'> type类型为type
print(type(object))  # <class 'type'> object类型为type

# 还可以使用内置属性__class__来查询对象的数据类型
print(type.__class__)  # <class 'type'> type类型为type
print(object.__class__)  # <class 'type'> object类型为type

(5)使用__base__查看类(class)继承的父类

print(int.__base__) # <class 'object'>
print(str.__base__)  # <class 'object'>
print(list.__base__) # <class 'object'>
print(type.__base__) # <class 'object'>
# 上面的类都继承于object类
print(object.__base__) # None,object类的父类为空

看不懂,没关系,慢慢来。不要想着通过这一次的学习就试图掌握所有的特性。

  • 23
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值