首页 > 编程语言 >python 类初始化的注意事项

python 类初始化的注意事项

时间:2023-01-11 19:45:08浏览次数:34  
标签:__ 初始化 width python self height color depth 注意事项

自己写了一个 Chessboard 类:

class Chessboard(VGroup):
    def __init__(
        self,
        shape: tuple = (8, 8),
        height: float = 1,
        width: float = 2,
#         depth 属性指的是厚度,不是指处于 z 轴的高度
        depth: float = 3,
        color: Color = BLUE,
        sheen: float = 0.2,
        **kwargs
    ):
        super().__init__(**kwargs)
        self.shape = shape
        self.height = height
        self.width = width
        self.depth = depth
        self.color = color
        self.sheen = sheen
        nr, nc = self.shape
        cube = Cube().set_sheen(self.sheen).set_color(self.color)
#         replace 方法指的是 mobject1 移动到 mobject2 的位置,且大小和 mobject2 一致
        self.add(*[cube.copy() for x in range(nc * nr)])
        self.arrange_in_grid(nr, nc, buff=1)
        self.stretch_to_fit_height(self.height)
        self.stretch_to_fit_width(self.width)
        self.stretch_to_fit_depth(self.depth)

但是 stretch_to_fit 函数没有生效,最后发现在 __init__ 函数中使用传入的属性时最好不要用 self.attribute 的形式,例如 self.height,而是之间使用 height

另外,代码部分最好写在 super().__init__() 函数后面,否则会报错

标签:__,初始化,width,python,self,height,color,depth,注意事项
From: https://www.cnblogs.com/daxiangcai/p/17044749.html

相关文章

  • Python学习笔记-常用模块介绍--时间模块
    1.时间模块分为哪三种格式?1.时间戳2.格式化字符串3.结构化时间 2.时间的示例#1.时间戳---常用于运算的print(time.time())#2.格式化字符串---用于显示,方......
  • Python学习笔记-常用模块介绍--猴子补丁
    1.什么是猴子补丁?属性在运行时的动态替换,叫做猴子补丁(MonkeyPatch)【发音ˈmʌŋkipætʃ】是一种思想,应用于团队引用了公共模块,想丰富模块,就在入口打这种“猴子补......
  • Python环境配置
    打开命令窗口三种方法:1.win+rcmd2.我的电脑cmd3.搜索命令提示符打开输入python进入交互式界面pipinstall(安装)request(爬虫模块)输入exit()退出pip下载他人的代码使用(类......
  • 【接口自动化测试】Python基础-字符串格式化
    """字符串格式化"""name='Jenny'age=30x="mynameis%s,ageis20"%nameprint(x)x1="mynameis%s,ageis%s"%(name,age)print(x1)#第二种y="myname......
  • Python运算符
    假设a=10b=201.算数运算符运算符描述+加-减*乘/除%取余数**幂——次方//取整除 2.比较(关系)运算符运算符描述==等于......
  • python:批量修改文件名批量修改图片尺寸
    批量修改文件名 参考博客:https://www.cnblogs.com/zf-blog/p/7880126.html功能:批量修改文件名12345678910111213141516171819202122......
  • Appium+python之APP自动化二(app启动)
    使用python编写脚本对app完成自动启动的操作准备工作参考前篇:Appium+python之APP自动化一一、python启动app的配置fromappiumimportwebdriver#后续操作依赖于这个......
  • 基于python的小波阈值去噪算法
    小波图像去噪原理图像和噪声在经小波变换后具有不同的统计特性:图像本身的能量对应着幅值较大的小波系数,主要集中在低频(LL)部分;噪声能量则对应着幅值较小的小波系数,并分散在......
  • 初始化获取Excel表头
    工具类中获取title方法/*初始化获取表头*/publicstaticList<String>getTitles(MultipartFilefile)throwsIOException{try(InputStreaminputStream......
  • PYTHON画三维图
    importmatplotlib.pyplotaspltfrommpl_toolkits.mplot3dimportAxes3Dimportnumpyasnp#bbb=[1if(i<=0.5ori>=1.5)else0foriinDdeffun(x,y):val......