class Rectangle(): def getperi(self,a,b): return(a+b)*2 def getArea(self,a,b): return a*b rect = Rectangle() print(rect.getperi(3,4)) print(rect.getArea(3,4)) print(rect.__dict__)
rect.__dict__ 返回定义的类的 属性 返回的是{}。也就是没有属性。
class Rectangle(): def __init__(self,a,b): self.a = a self.b = b def getPeri(self): return (self.a + self.b)*2 def getArea(self): return(self.a * self.b) rect = Rectangle(3,4) print(rect.getPeri()) print(rect.getArea()) print(rect.__dict__)
返回类的属性是{'a': 3, 'b': 4}
搬运下面帖
(18条消息) python学习——类中为什么要定义__init__()方法_geerniya的博客-CSDN博客
标签:__,9.24,python,self,Rectangle,def,print,rect From: https://www.cnblogs.com/jiujiangwu/p/16725968.html