Python 类 - 通过迭代列表来分配对象的值

标签 python class object iteration

我正在编写一个程序,需要一个函数来跟踪多个交易所中几种不同货币钱包的模拟值(value)。我编写了一个类来声明、保存和检索模拟余额:

>>> class Balance:
    def __init__(self,exchange,currency):
        self.exchange = exchange
        self.currency = currency
    def update(self,val):
        self.val = val
    def get(self):
        qty = self.val
        if qty > 0:
            return qty
        else:
            return False

每个“balance keeper”对象都由 currency_exchange 引用,如下所示。手动声明对象时,一切正常:

>>> BTC_poloniex=Balance('poloniex','BTC')
>>> BTC_poloniex.update(5.5)
>>> BTC_poloniex.get()
5.5
>>> XMR_poloniex=Balance('poloniex,'XMR')
>>> XMR_poloniex.update(11)
>>> XMR_poloniex.get()
11

但是,我想迭代以下列表并动态声明对象并分配值,如下所示:

>>> CURRENCIES=['BTC','ETH','XRP','XLM','NEO','DASH','XMR','LSK','ZEC','LTC','ETC', 'ADA', 'XMR']
>>> BALANCES = [("BTC", 5.0), ("DASH", 10.0), ("ETH", 10.0), ("XRP", 100.0), ("XLM", 1000.0), ("NEO", 100.0), ("XMR", 10.0), ("LSK", 1000.0), ("ZEC", 10.0), ("LTC", 20.0), ("ETC", 20.0), ("ADA", 1000.0)]
>>> EXCHANGES=["poloniex", "cex", "bittrex", "binance", "okex"]
>>> for ex in EXCHANGES:
    for cur in CURRENCIES:
        balance_keeper = cur+'_'+ex
        balance_keeper=Balance(ex,cur)
        for balance in BALANCES:
            if balance[0] == cur:
                val = balance[1]
                print(str(ex)+str('_')+str(cur)+str(' updated to : ')+str(+val))
                balance_keeper.update(val)
                balance_keeper.get()


poloniex_BTC updated to : 5.0
5.0
poloniex_ETH updated to : 10.0
10.0
poloniex_XRP updated to : 100.0
100.0
poloniex_XLM updated to : 1000.0
1000.0
poloniex_NEO updated to : 100.0
100.0
poloniex_DASH updated to : 10.0
10.0
poloniex_XMR updated to : 10.0
10.0
poloniex_LSK updated to : 1000.0
1000.0
poloniex_ZEC updated to : 10.0
10.0
poloniex_LTC updated to : 20.0
20.0
poloniex_ETC updated to : 20.0
20.0
poloniex_ADA updated to : 1000.0
1000.0
poloniex_XMR updated to : 10.0
10.0
cex_BTC updated to : 5.0
5.0
cex_ETH updated to : 10.0
10.0
cex_XRP updated to : 100.0
100.0
cex_XLM updated to : 1000.0
1000.0
cex_NEO updated to : 100.0
100.0
cex_DASH updated to : 10.0
10.0
cex_XMR updated to : 10.0
10.0
cex_LSK updated to : 1000.0
1000.0
cex_ZEC updated to : 10.0
10.0
cex_LTC updated to : 20.0
20.0
cex_ETC updated to : 20.0
20.0
cex_ADA updated to : 1000.0
1000.0
cex_XMR updated to : 10.0
10.0
bittrex_BTC updated to : 5.0
5.0
bittrex_ETH updated to : 10.0
10.0
bittrex_XRP updated to : 100.0
100.0
bittrex_XLM updated to : 1000.0
1000.0
bittrex_NEO updated to : 100.0
100.0
bittrex_DASH updated to : 10.0
10.0
bittrex_XMR updated to : 10.0
10.0
bittrex_LSK updated to : 1000.0
1000.0
bittrex_ZEC updated to : 10.0
10.0
bittrex_LTC updated to : 20.0
20.0
bittrex_ETC updated to : 20.0
20.0
bittrex_ADA updated to : 1000.0
1000.0
bittrex_XMR updated to : 10.0
10.0
binance_BTC updated to : 5.0
5.0
binance_ETH updated to : 10.0
10.0
binance_XRP updated to : 100.0
100.0
binance_XLM updated to : 1000.0
1000.0
binance_NEO updated to : 100.0
100.0
binance_DASH updated to : 10.0
10.0
binance_XMR updated to : 10.0
10.0
binance_LSK updated to : 1000.0
1000.0
binance_ZEC updated to : 10.0
10.0
binance_LTC updated to : 20.0
20.0
binance_ETC updated to : 20.0
20.0
binance_ADA updated to : 1000.0
1000.0
binance_XMR updated to : 10.0
10.0
okex_BTC updated to : 5.0
5.0
okex_ETH updated to : 10.0
10.0
okex_XRP updated to : 100.0
100.0
okex_XLM updated to : 1000.0
1000.0
okex_NEO updated to : 100.0
100.0
okex_DASH updated to : 10.0
10.0
okex_XMR updated to : 10.0
10.0
okex_LSK updated to : 1000.0
1000.0
okex_ZEC updated to : 10.0
10.0
okex_LTC updated to : 20.0
20.0
okex_ETC updated to : 20.0
20.0
okex_ADA updated to : 1000.0
1000.0
okex_XMR updated to : 10.0
10.0

我遇到的问题是变量 balance_keeper 没有达到我的预期。例如,请查看上面的最后两行。

不是初始化对象 okex_ADA,然后初始化一个新对象 okex_XMR,而是每次迭代都会覆盖该对象以及 Balance_keeper 应该保持根本没有被初始化;相反,循环只是不断将对象分配给垃圾变量名称“balance_keeper”,而不是它应该保存的变量(例如 okex_XMR):

>>> okex_XMR.get()
Traceback (most recent call last):
  File "<pyshell#73>", line 1, in <module>
    okex_XMR.get()
NameError: name 'okex_XMR' is not defined

正如您所看到的,它不是创建一个对象,而是在迭代时覆盖以前的对象:

>>> balance_keeper.get()
10.0

我做错了什么?如何迭代这些列表,并正确初始化每个变量,而不是简单地覆盖占位符变量?

最佳答案

使用字典通过分配字符串名称来存储对象

datadict = {}
for ex in EXCHANGES:
    for cur in CURRENCIES:
        ex_cur_name = cur+'_'+ex
        datadict[ex_cur_name] = Balance(ex, cur)

然后您可以通过以下方式访问对象:

>>>datadict[ex_cur_name]

关于Python 类 - 通过迭代列表来分配对象的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54032179/

相关文章:

Python:从一月到当月动态选择列

typescript - 可以省略 Typescript 类上的泛型类型

javascript - 使用已删除的值创建新数组(React Native)

javascript对象在html页面上工作但无法在wordpress中加载

javascript - 显示 [object Object] 而不是对象属性

python - 完全抑制 tcpdump 的输出

python - Selenium python 点击​​span类

python - 从 Shopify API 获取详细信息并关闭订单

c# - 在 C# 中引用嵌套类对象

来自另一个没有静态的类的Android调用函数