python - 哎呀。正确传递参数
问题描述
这有效:
class Thing():
def __init__(self):
a=2
exec(f'self.foo ={a} + 2')
x = Thing()
print(x.foo)
这不起作用:
a=2
class Thing(a):
def __init__(self, a):
exec(f'self.foo ={a} + 2')
x = Thing(a)
print(x.foo)
问题:如何使第二个示例正常工作(应该放在2
里面x
)?
解决方案
class Thing: #Note: Not Thing(a)
def __init__(self, a):
self.foo = a + 2
推荐阅读
- python - 类型错误:索引()缺少 1 个必需的位置参数:'subject_slug'
- javascript - 如何为 firebase 功能选择不同的数据库?
- reactjs - 如何纠正打字稿错误?(道具类型库)
- python - 使用 html 编译 jinja 2 时出错
- algorithm - 多个依赖的自动完成
- java - 服务器和客户端之间的套接字对象不同
- angular - 来自错误验证的 Angular 11 反应性不起作用
- c++ - 没有 void_t 的 SFINAE(可能是模板专业化问题)
- python - BeautifulSoup 不采用完整的 HTML 代码
- c++ - 将音频流写入循环缓冲区但分段错误读取值