加入类属性的字典 [英] Accesing dictionary with class attribute
问题描述
假设我有一个dict,
config = {'account_receivable': ''''''''''''''''''''''''''''''''''''''' :$ 3
','account_inventory':'2','account_accumulated_depriciation':'34',' 'account_
depriciation_expense':'35','account_salary_expense':'30','account_payroll_pa
yable':'68','account_discount':'36','financial_year_close' -08'}
如果print - > config ['account_receivable']它将返回其对应的值那个4
但是我想通过这种方式访问它 - > config.account_receivable,然后它将返回相应的值
我如何实现这个?
如果有人可以帮助我
BR //
nazmul
为此目的,多年前,我发明了简单的 Bunch
成语;一个简单的方法来实现 Bunch
是:
class Bunch(object) :
def __init __(self,adict):
self .__ dict __。update(adict)
如果 config
是一个dict,你不能使用 config.account_receivable
- 这绝对不可能,因为dict不 那个属性,句点。但是,您可以将 config
换成 Bunch
:
cb = Bunch(config)
然后访问 cb.config_account
到您心中的内容!
修改:如果您希望 Bunch
中的属性分配也会影响原始 dict
( config
在这种情况下),所以例如 cb.foo = 23
将执行 config ['foo'] = 23
,您需要一个不同寻常的 Bunch
:
class RwBunch(object):
def __init__ (self,adict):
self .__ dict__ = adict
通常, c $ c> Bunch 是首选,正是因为 ,在实例化之后, Bunch
实例和 dict
它被完全从完全解耦 - 其中任何一个的改变不影响另一个;而最常见的是这样的解耦。
当您执行需要耦合效果时,则 RwBunch
是获取它们的方式:使用它,实例上的每个属性设置或删除将从 dict
内在地设置或删除该项目, ,反之亦然,从 dict
中设置或删除项目将从实例本质上设置或删除属性。
now i am working with python. So one question about dict .... suppose i have a dict that
config = {'account_receivable': '4', 'account_payable': '5', 'account_cogs': '8', 'accoun
t_retained_earning': '9', 'account_income': '6', 'account_expense': '31', 'durat
ion': 2, 'financial_year_month': 9, 'financial_year_day': 15, 'account_cash': '3
', 'account_inventory': '2', 'account_accumulated_depriciation': '34', 'account_
depriciation_expense': '35', 'account_salary_expense': '30', 'account_payroll_pa
yable': '68', 'account_discount': '36', 'financial_year_close': '2008-08-08'}
if print --> config['account_receivable'] it will return its corresponding value that 4
but i want to access it by that way--> config.account_receivable and then it will return it corresponding value
how i can implement this??? if any one can please help me
BR// nazmul
For that purpose, lo that many years ago, I invented the simple Bunch
idiom; one simple way to implement Bunch
is:
class Bunch(object):
def __init__(self, adict):
self.__dict__.update(adict)
If config
is a dict, you can't use config.account_receivable
-- that's absolutely impossible, because a dict doesn't have that attribute, period. However, you can wrap config
into a Bunch
:
cb = Bunch(config)
and then access cb.config_account
to your heart's content!
Edit: if you want attribute assignment on the Bunch
to also affect the original dict
(config
in this case), so that e.g. cb.foo = 23
will do config['foo'] = 23
, you need a slighly different implementation of Bunch
:
class RwBunch(object):
def __init__(self, adict):
self.__dict__ = adict
Normally, the plain Bunch
is preferred, exactly because, after instantiation, the Bunch
instance and the dict
it was "primed" from are entirely decoupled -- changes to either of them do not affect the other; and such decoupling, most often, is what's desired.
When you do want "coupling" effects, then RwBunch
is the way to get them: with it, every attribute setting or deletion on the instance will intrinsically set or delete the item from the dict
, and, vice versa, setting or deleting items from the dict
will intrinsically set or delete attributes from the instance.
这篇关于加入类属性的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!