- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个程序,需要一个函数来跟踪多个交易所中几种不同货币钱包的模拟值(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/
我的一位教授给了我们一些考试练习题,其中一个问题类似于下面(伪代码): a.setColor(blue); b.setColor(red); a = b; b.setColor(purple); b
我似乎经常使用这个测试 if( object && object !== "null" && object !== "undefined" ){ doSomething(); } 在对象上,我
C# Object/object 是值类型还是引用类型? 我检查过它们可以保留引用,但是这个引用不能用于更改对象。 using System; class MyClass { public s
我在通过 AJAX 发送 json 时遇到问题。 var data = [{"name": "Will", "surname": "Smith", "age": "40"},{"name": "Wil
当我尝试访问我的 View 中的对象 {{result}} 时(我从 Express js 服务器发送该对象),它只显示 [object][object]有谁知道如何获取 JSON 格式的值吗? 这是
我有不同类型的数据(可能是字符串、整数......)。这是一个简单的例子: public static void main(String[] args) { before("one"); }
嗨,我是 json 和 javascript 的新手。 我在这个网站找到了使用json数据作为表格的方法。 我很好奇为什么当我尝试使用 json 数据作为表时,我得到 [Object,Object]
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我听别人说 null == object 比 object == null check 例如: void m1(Object obj ) { if(null == obj) // Is thi
Match 对象 提供了对正则表达式匹配的只读属性的访问。 说明 Match 对象只能通过 RegExp 对象的 Execute 方法来创建,该方法实际上返回了 Match 对象的集合。所有的
Class 对象 使用 Class 语句创建的对象。提供了对类的各种事件的访问。 说明 不允许显式地将一个变量声明为 Class 类型。在 VBScript 的上下文中,“类对象”一词指的是用
Folder 对象 提供对文件夹所有属性的访问。 说明 以下代码举例说明如何获得 Folder 对象并查看它的属性: Function ShowDateCreated(f
File 对象 提供对文件的所有属性的访问。 说明 以下代码举例说明如何获得一个 File 对象并查看它的属性: Function ShowDateCreated(fil
Drive 对象 提供对磁盘驱动器或网络共享的属性的访问。 说明 以下代码举例说明如何使用 Drive 对象访问驱动器的属性: Function ShowFreeSpac
FileSystemObject 对象 提供对计算机文件系统的访问。 说明 以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读
我是 javascript OOP 的新手,我认为这是一个相对基本的问题,但我无法通过搜索网络找到任何帮助。我是否遗漏了什么,或者我只是以错误的方式解决了这个问题? 这是我的示例代码: functio
我可以很容易地创造出很多不同的对象。例如像这样: var myObject = { myFunction: function () { return ""; } };
function Person(fname, lname) { this.fname = fname, this.lname = lname, this.getName = function()
任何人都可以向我解释为什么下面的代码给出 (object, Object) 吗? (console.log(dope) 给出了它应该的内容,但在 JSON.stringify 和 JSON.parse
我正在尝试完成散点图 exercise来自免费代码营。然而,我现在只自己学习了 d3 几个小时,在遵循 lynda.com 的教程后,我一直在尝试确定如何在工具提示中显示特定数据。 This code
我是一名优秀的程序员,十分优秀!