-1

I would like to define a parameter variable via the default attribute id_in_group of the player. However, this attribute does not seem to be accessible via the ways I could think of (such as via BasePlayer.id_in_group).

The code of the class player:

class Player(BasePlayer):
    investment_amount = models.CurrencyField(
        doc="""
        Amount invested by this player.
        """,
        min=0, max=Constants.endowment
    )
    random_number = BasePlayer.id_in_group

    def set_payoffs(self):
        for p in self.get_players():
            p.payoff = 110

How could I access the attribute id_in_group? Or is it impossible due to the fact that it is a default attribute preset by oTree?

RexE
  • 17,085
  • 16
  • 58
  • 81
Aqqqq
  • 816
  • 2
  • 10
  • 27

2 Answers2

2

in oTree the id_in_group attribute is assigned after the session started. It is quite logical because the groups haven't been formed yet before the session started.

You are trying to assign a value in the Player class declaration, when none of players have been assigned to groups, thus no one has his/her id_in_group.

Moreover, you assign a value to a property of the model, not to the fields of specific instances (i.e. players). If you need to assign to random_id field of each Player a specific value based on his/her id in group do the following:

in models.py:

class Player(BasePlayer):
     random_id = models.IntegerField()

class Subsession(BaseSubsession):
     def before_session_starts(self):
         for p in self.get_players():
             p.random_id = p.id_in_group 
Philipp Chapkovski
  • 1,949
  • 3
  • 22
  • 43
-1

Have you tried accessing the variable via super.id_in_group or self.id_in_group? If it's a variable of the parent class, you wouldn't call the class directly like you are doing.

Also, Google has a ton of results for how to access parent class variables, a lot of which are answers from stackoverflow that explain things really well :-)

Michael Platt
  • 1,297
  • 12
  • 25
  • When I used "super", the error was: type object 'super' has no attribute 'id_in_group'; when I used 'self', the error was: Unresolved reference 'self'. I don't think it has anything to do with parent class. But thanks for the answer. – Aqqqq Aug 24 '16 at 14:32
  • Can you post the BasePlayer class code? It seems odd that `self` wouldn't solve problem assuming you have your inheritance set correctly. – Michael Platt Aug 24 '16 at 14:33
  • `super` is a function, and wouldn't make sense at that point anyway; `self` is only available inside a method. – Daniel Roseman Aug 24 '16 at 14:46
  • @MichaelPlatt BasePlayer is prewritten by otree developers. I don't know where it is. – Aqqqq Aug 24 '16 at 14:51