天宫鹤

Python中一切皆为对象,这样理解!

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

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

原文链接:https://blog.csdn.net/m0_46610658/article/details/140901698
"""
# %%
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

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


print("--------函数---------")
print(f"函数的唯一标识符id = {id(say_hello)}")
print(f"函数的类型type = {type(say_hello)}")
print(f"函数的值value = {say_hello}")
# 为函数设置 short_description 属性
say_hello.short_description = "我是一个函数噢!"
print('为函数设置的属性:', say_hello.short_description)

# 输出结果:
# --------函数---------
# 函数的唯一标识符id = 2597574073120
# 函数的类型type = <class 'function'>
# 函数的值value = <function say_hello at 0x0000025CCB7ADB20>
# 为函数设置的属性: 我是一个函数噢!


# %%


class Student:
    pass
    def __str__(self):
        return "一个类对象/实例。"


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

# 输出结果:
# --------类---------
# 类的唯一标识符id = 2597545335424
# 类的类型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 = 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之间到底是什么关系?

原文链接:https://blog.csdn.net/m0_46610658/article/details/140901698
"""
# %%

# 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类的父类为空
复制代码

参考网址:https://blog.csdn.net/m0_46610658/article/details/140901698

posted on   GoGrid  阅读(128)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示