4 ms·
class Bprime(object): def __init__(self, a): for var in dir(a): setattr(self, var, getattr(a, var)) Has anyone ever done such a th
by gweinberg 9y ago
class Bprime(object):
def __init__(self, a):
for var in dir(a):
setattr(self, var, getattr(a, var))
Has anyone ever done such a thing? Why?
- thesmallestcat 9y agoIt's blindingly wrong. It creates a mutable snapshot of the other object. Depending on the size of `a` and how many instances are being wrapped in this way, it could increase memory footprint substantially. I can't imagine a time where you'd want to do this. This sort of effect is usually achieved by a __getattribute__ that falls back to whichever object(s) are being shadowed. Not that that's ideal, but weird to see somebody who knows Python so well throw out such a useless example.