[Linuxtrent] python: traits e pickle

  • From: Emanuele Olivetti <olivetti@xxxxxx>
  • To: linuxtrent <linuxtrent@xxxxxxxxxxxxx>
  • Date: Thu, 6 Nov 2003 19:22:11 +0100

Visto che c'e' qualche esperto di Python Traits (e di Python) vi
chiedo questo: perche' la serializzazione di una classe che usa
i Traits mi da errore ("TypeError: can't pickle function objects")?

Esempio:
---------------------------------------------------
import traits
import pickle

class Persona(traits.HasTraits):
         __traits__={
            'nome' : 'unknown',
            'cognome' : 'unknown',
            }

if __name__=='__main__':
    p=Persona(nome='Emanuele', congnome='Olivetti')
    a=pickle.dumps(p)
    print a
---------------------------------------------------

Mi dice che non posso fare il pickling dell'istanza 'p':

TypeError: can't pickle function objects

Perche'?

Riporto qui di seguito il log completo dell'errore:

-----
Traceback (most recent call last):
  File "traits-test.py", line 13, in ?
    a=pickle.dumps(p)
  File "/opt/python2.3/lib/python2.3/pickle.py", line 1386, in dumps
    Pickler(file, protocol, bin).dump(obj)
  File "/opt/python2.3/lib/python2.3/pickle.py", line 231, in dump
    self.save(obj)
  File "/opt/python2.3/lib/python2.3/pickle.py", line 293, in save
    f(self, obj) # Call unbound method with explicit self
  File "/opt/python2.3/lib/python2.3/pickle.py", line 739, in save_inst
    save(stuff)
  File "/opt/python2.3/lib/python2.3/pickle.py", line 293, in save
    f(self, obj) # Call unbound method with explicit self
  File "/opt/python2.3/lib/python2.3/pickle.py", line 663, in save_dict
    self._batch_setitems(obj.iteritems())
  File "/opt/python2.3/lib/python2.3/pickle.py", line 677, in _batch_setitems
    save(v)
  File "/opt/python2.3/lib/python2.3/pickle.py", line 313, in save
    rv = reduce(self.proto)
  File "/opt/python2.3/lib/python2.3/copy_reg.py", line 69, in _reduce_ex
    raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle function objects
-------
-- 
Per iscriversi  (o disiscriversi), basta spedire un  messaggio con OGGETTO
"subscribe" (o "unsubscribe") a mailto:linuxtrent-request@xxxxxxxxxxxxx


Other related posts: