Python全系列 教程
3567个小节阅读:5931.1k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
object
类是所有类的父类,因此所有的类都有object
类的属性和方法。我们显然有必要深入研究一下object
类的结构。对于我们继续深入学习Python很有好处。
dir()
查看对象属性为了深入学习对象,先学习内置函数dir()
,他可以让我们方便的看到指定对象所有的属性
【测试】查看对象所有属性以及和object
进行比对
xxxxxxxxxx
class Person:
def __init__(self,name,age):
self.name = name
self.age = age
def say_age(self):
print(self.name,"的年龄是:",self.age)
obj = object()
print(dir(obj))
s2 = Person("高淇",18)
print(dir(s2))
运行结果:
xxxxxxxxxx
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'age', 'name', 'say_age']
从上面我们可以发现这样几个要点:
Person
对象增加了六个属性:
__dict__
__module__
__weakref__
age
name
say_age
object
的所有属性,Person
类作为object
的子类,显然包含了所有的属性
我们打印age
、name
、say_age
,发现say_age
虽然是方法,实际上也是属性。只不过,这个属性的类型是method
而已。
xxxxxxxxxx
age <class 'int'>
name <class 'str'>
say_age <class 'method'>
⚠️关于object这些属性的详细学习,会后面学习中逐个涉及。在此,没必要展开。
实时效果反馈
1. 如下关于object类,说法错误的是:
A object
类是所有类的父类。
B 内置函数dir()
可以方便的查看指定对象所有的属性
C object
类,也可以是大写的Object
类
D 所有的类都有object
类的属性和方法
答案
1=>C