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 中加不加一个样

本文探讨了Python中类的定义方式,包括继承object基类与否的区别,通过实例展示了如何定义类属性和方法,以及如何访问和使用这些属性。同时,对比了不同类的内置属性数量,揭示了Python类的底层实现机制。

被折叠的 条评论
为什么被折叠?



