oTree Forum >

Keeping custom class objects in memory (without it being pickled)

#1 by aseyq

I am developing an experiment (potentially a package) to implement some chain structure. The most useful way for me to create a custom class with its own methods, etc. However, I would like to store my objects in a place I can reach throughout my experiment. My first instinct of course was to store them in session variables.

def some_player_function(player):
    new_chain = new Chain()
When I do that, my object is stored as "pickle". For that reason: 

a) I cannot link to the same object. The pickle takes basically a copy of an instance, rather than linking to the object.
b) If my objects have links to oTree objects (player, group, etc.) I get an error because it cannot turn to pickle object.

I can think of several workarounds but I am wondering whether I can keep instances of objects in memory which I can reach throughout the experiment.


#2 by Chris_oTree

Things like that are going to be tricky. I recommend instead to reference players by id_in_subsession rather than having a reference to the Player instance.

#3 by aseyq

Thank you so much, Chris. That is very helpful and solves the linking the player/group objects for me. Also, I implemented an ExtraModel instead of a standard class for my custom objects and I could reach them rather easily.

Write a reply

Set forum username