[英]Creating a list from the class attributes - Python
我想知道是否有人可以详细说明为什么从 class 中传递的两个属性中创建一个新列表会返回“NoneType”?
class Finance:
def __init__(self, market = '^GSPC', tickers = []):
self.market = market
self.tickers = tickers
self.final_list = self.tickers.insert(0,self.market)
x = Finance(tickers = ['AMZN', 'AAPL'])
type(x.final_list)
我找到了一种通过使用创建这两个列表的方法
[value for value in self.__dict__.values()]
但我只是觉得应该有一种更优雅的方式来做到这一点。
好吧,我已经尝试使用 Python 3.10,但我很确定插入列表方法已经到位并且不会返回值。 所以我的建议是这样更改代码:
import copy
class Finance:
def __init__(self, market = '^GSPC', tickers = []):
self.market = market
self.tickers = tickers
self.tickers.insert(0,self.market)
self.final_list = copy.deepcopy(self.tickers)
现在您在变量实例 final_list 中拥有修改列表的深层副本
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.