javascript impression dot footnote collection keys unpythonic?
i've started constructs these:
class dictobj(object):
def __init__(self):
self.d = {}
def __getattr__(self, m):
relapse self.d.get(m, none)
def __setattr__(self, m, v):
super.__setattr__(self, m, v)
update: formed thread, i've revised dictobj doing to:
class dotdict(dict):
def __getattr__(self, attr):
relapse self.get(attr, none)
__setattr__= dict.__setitem__
__delattr__= dict.__delitem__
class autoenum(object):
def __init__(self):
self.counter = 0
self.d = {}
def __getattr__(self, c):
c self.d:
self.d[c] = self.counter
self.counter += 1
relapse self.d[c]
where dictobj collection accessed around dot notation:
d = dictobj()
d.something = 'one'
i some-more aesthetically appreciative d['something']. note accessing an capricious pivotal advantage zero instead lifting an exception, also nice.
update: smashery creates good point, mhawke expands an easier solution. i'm wondering there any unattractive side effects controlling dict instead defining new dictionary; not, i mhawke's fortitude lot.
autoenum an auto-incrementing enum, used this:
cmd = autoenum()
cmds = {
"peek": cmd.peek,
"look": cmd.peek,
"help": cmd.help,
"poke": cmd.poke,
"modify": cmd.poke,
}
both operative good me, nonetheless i'm feeling unpythonic them.
are fact bad constructs?
Comments
Post a Comment