New submission from Alexander Neumann <>:

The code below causes a 

   AttributeError: 'Event' object has no attribute 'name'

It seems like the entries of `` are not available during 
`__setstate__`. However, the behaviour depends on the Python version.

For Python 3.6.4 it's always the 'first' (as in it's always 'to_A' which has 
been added first) element in `` that is incomplete. For Python 
3.4.8 and 3.5.5 it looks like a racing condition since sometimes there is no 
error, sometimes its the first or the second element which is incomplete.
Letting `Machine` serve as its own model and pickling that does work for all 
three versions.

Tested on MacOS High Sierra.

import pickle
from functools import partial

class Event:

    def __init__(self, name, machine): = name
        self.machine = machine

    def trigger(self):

class Machine:

    def __init__(self, model, states): = {}
        self.model = model if model != 'self' else self

        for state in states:
            trigger = 'to_%s' % state
  [trigger] = Event(trigger, self)
            trig_func = partial([trigger].trigger)
            setattr(self.model, trigger, trig_func)

    def __getstate__(self):
        return self.__dict__

    def __setstate__(self, state):

class Model:

    def __init__(self):
        self.machine = Machine(self, states=['A', 'B'])

model = Model()
dump = pickle.dumps(model)
model2 = pickle.loads(dump)

components: Interpreter Core
messages: 315127
nosy: aleneum
priority: normal
severity: normal
status: open
title: Pickling objects with recursive references and partials fail
type: behavior
versions: Python 3.4, Python 3.5, Python 3.6

Python tracker <>
Python-bugs-list mailing list

Reply via email to