相关疑难解决方法(0)

为什么我不能腌制这个物体?

我有一个班级(下):

class InstrumentChange(object):
    '''This class acts as the DTO object to send instrument change information from the
       client to the server. See InstrumentChangeTransport below
    '''
    def __init__(self, **kwargs):
        self.kwargs = kwargs
        self._changed = None

    def _method_name(self, text):
        return text.replace(' ','_').lower()

    def _what_changed(self):
        ''' Denotes the column that changed on the instrument returning the column_name of what changed.'''
        if not self._changed:
            self._changed = self._method_name(self.kwargs.pop('What Changed'))

        return self._changed

    def __getattr__(self, attr):
        for key in self.kwargs.iterkeys():
            if self._method_name(key) == attr:
                return self.kwargs[key]

    def …
Run Code Online (Sandbox Code Playgroud)

python

14
推荐指数
2
解决办法
2万
查看次数

如何pickle从dict派生的python对象

这是我需要挑选的课程:

class sdict(dict):
    def __getattr__(self, attr):
        return self.get(attr, None)
    __setattr__= dict.__setitem__
    __delattr__= dict.__delitem__
    __reduce__ = dict.__reduce__
Run Code Online (Sandbox Code Playgroud)

在我看来__reduce__应该照顾酸洗,但相反:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/home/daniyar/work/Apr24/<ipython-input-28-1cc94b920737> in <module>()
----> 1 pickle.dumps(sdict(a=1))

/usr/lib/python2.6/pickle.pyc in dumps(obj, protocol)
   1364 def dumps(obj, protocol=None):
   1365     file = StringIO()
-> 1366     Pickler(file, protocol).dump(obj)
   1367     return file.getvalue()
   1368 

/usr/lib/python2.6/pickle.pyc in dump(self, obj)
    222         if self.proto >= 2:
    223             self.write(PROTO + chr(self.proto))
--> 224         self.save(obj)
    225         self.write(STOP)
    226 

/usr/lib/python2.6/pickle.pyc in save(self, obj)
    304             reduce = getattr(obj, "__reduce_ex__", …
Run Code Online (Sandbox Code Playgroud)

python pickle

2
推荐指数
1
解决办法
1970
查看次数

标签 统计

python ×2

pickle ×1