【发布时间】:2015-11-19 23:10:17
【问题描述】:
对于我正在进行的项目,我需要将一个字符串(具有 ID 号(即 ID_00000001...ID_00002032 或类似的东西))转换为一个类。 我的意思是,已经存储了一个具有如下值的类:
class ID_00000001:
name = 'Robert Johnson'
age = 193
facebook_url = 'facebook.com/Robert_johnson'
etc.
我想这样做,以便我可以将此个人资料与其他人进行比较。我正在考虑这样做的方式是比较(psudo)之类的值:
current_profile_str = 'ID_00000001'
for i in range(len(all_IDs)):
matches = []
num = *converts number into 8 digit str*
cycle_profile_str = 'ID_' + num
*convert current_profile and cycle_profile to class*
if (current_profile.age == cycle_profile.age):
matches.append(cycle_profile_str)
所以我想我在这种情况下的问题是,我如何能够将字符串转换为类?
【问题讨论】:
-
你为什么不用字典?
-
我感觉你在使用类有点奇怪....
-
看起来您将类与实例混淆了。在继续之前最好read about it
-
以这种方式使用类是可疑的......你能进一步解释一下你的用例吗?
标签: python string class backend