Suppose I have a class, and I want to reference some elements in the ‘ __dict__ (for instance, I want to copy the dict and delete the attribute that cannot be pickled), from inside the class.
Problem is, those attributes are “private” so my code ends up looking like so
class MyClasss(object):
def __init__(self):
self.__prv=1
def __getstate__(self):
ret=self.__dict__.copy()
del ret['_MyClass__prv']
I reference the class name explicitly in the del statement, which looks a little ugly for me.
Is there something nicer? something like MyClass.getPrivateString('prv')
Of course I can implement one myself, but I would be surprised if there isn’t a builtin to surpass this problem.
At the end, I used a variant of thieger’s solution
I think this is the most robust way I can write that piece of code, aside from giving up the mangling, which might be the right thing to do, but I was asking what to do in case you actually have mangling 🙂