python2中为什么在进行类定义时最好要加object
python2 进行 为什么 定义 object 最好
2023-09-14 09:15:51 时间
class Student(object):
def __init__(self, name, score):
self.name = name
self.score = score
def print_score(self):
print('%s: %s' % (self.name, self.score))
lg=Student('lg',99)
lg.name
lg.print_score()
class Person:
"""
不带object
"""
name = "zhengtong"
class Animal(object):
"""
带有object
"""
name = "chonghong"
if __name__ == "__main__":
x = Person()
print ("Person", len(dir(x)))
print ("Person", dir(x))
y = Animal()
print ("Animal", len(dir(y)))
print ("Animal", dir(y))
lg: 99
Person 27
Person ['__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__', 'name']
Animal 27
Animal ['__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__', 'name']
object是一个基类,或称之为元类。在Python3 中加不加一个样
相关文章
- python2 和 python3 常见差异及兼容方式梳理
- Ubuntu16.04系统查看已安装的python版本,及Python2与Python3之间切换
- 安装搭建Python2.* 和3.* 环境详细步骤
- 当同时安装Python2和Python3后,如何兼容并切换使用详解(比如pip使用)
- python2与python3中input的区别
- Python3与Python2的一些区别
- python2装饰器_python内置装饰器
- python2装饰器_python进阶路线图
- 公司项目申请软著,看到同事在一行一行的复制,用python2分钟帮了他
- 黑帽编程应用之Python2
- python2 nonlocal_python非零返回
- python2 nonlocal_Python nonlocal
- python2 nonlocal_python关键字及用法
- CentOS7安装python3与python2共存
- Python2安装_python安装不了
- Linux系统自带Python2&yum的卸载及重装
- 如何使用Python2启动一个https服务器?
- 旧酒换新瓶,新版M1/M2芯片Macos系统(Ventura)安装古早版本Python2.7(Python2.x)
- Python2.x 与 3.x 版本区别
- centos7系统下python2与python3共存
- 把模块有关联的放在一个文件夹中 在python2中调用文件夹名会直接失败 在python3中调用会成功,但是调用不能成功的解决方案详解编程语言
- python的基本知识,range在python2.x中和python3.x中的区别详解编程语言
- python2.x和3.x的区别(不定时更新)详解编程语言
- Python3.x与Python2.x的区别详解编程语言
- Python3.x与Python2.x的差异用法详解编程语言
- Python3.x和Python2.x的区别介绍