Python类-通过遍历列表分配对象的值

时间:2019-01-04 01:48:40

标签: python class object iteration

我正在编写一个程序,该程序需要一个功能来跟踪跨多个交易所的几种不同货币钱包的模拟值。我编写了一个类来声明,持有和检索模拟的余额:

W = logsumexp(
    np.add.outer(Wm, Wm).swapaxes(1, 2)[(slice(None),)*2 + triu_inds],
    axis=-1  # Perform summation over last axis.
)
W = np.triu(W, k=1)

>>> 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 引用了每个“平衡者”对象,如下所示。手动声明对象时,一切正常:

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”,而不是应该保留的变量(例如okex_XMR):

balance_keeper

如您所见,它没有创建 new 对象,而是在迭代时覆盖了先前的对象:

>>> 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

我在做什么错?与简单地覆盖占位符变量相反,我该如何遍历这些列表并正确初始化每个变量?

2 个答案:

答案 0 :(得分:1)

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

anim.Play(Random.Range(0, list.Count))

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

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

答案 1 :(得分:1)

我认为您错过了对python语法的了解。看这段代码: balance_keeper = cur+'_'+ex。如我所见,您所期望的是balance_keeper会变成像okex_XMR这样的变量。不,它不能那样工作。

  1. 以上分配仅将balance_keeper分配给字符串。不是变量。请记住,cur+'_'+ex是一个表达式,它返回字符串!

  2. balance_keeper之后的balance_keeper=Balance(ex,cur)的名称将被覆盖。

  3. 这里的想法是保留多个钱包,每个钱包由一对货币和汇率标识。这导致使用字典,其键为(cur, ex)的元组。