0

is it possible in python to address class variables from other class variables within the same class?

My problem is: I am trying to prepare some static code, that would look like this:

class MyBaseObject:
  SIGNAL_NAME_1 = "signal-name-1"
  SIGNAL_NAME_2 = "signal-name-2"

  ALL_SIGNALS = {
    SIGNAL_NAME_1: ( signal-definition ),
    SIGNAL_NAME_2: ( signal-definition ) }

My problem with the above is that, according to python SIGNAL_NAME_1 and _2 are not defined while creating dict. Accessing them by MyBaseObject.SIGNAL_NAME_1 also does not work (unknown object).

So question is - is it possible to have class variables referring to one another in python?

Thank you!

Tomasz W
  • 1,841
  • 1
  • 16
  • 25

1 Answers1

1

No, there shouldn't be any problem referring to other class variables just using there names. You cannot however refer to MyBaseObject as that isn't defined until the class definition completes.

The code you posted will work just fine (if signal and definition are defined), so if you are getting complaints about names not being defined that means you didn't post the exact code you used. Try posting the exact code and the exact and complete error message.

Duncan
  • 92,073
  • 11
  • 122
  • 156
  • Argh, You are totally right! I kept getting errors, but apparently those were not related to class variables being addressed, but the doc snippet I inserted into the dict. Python kept telling me the error was in SIGNAL_NAME_1, whereas it was actually caused by the snippet. Thanks a lot - i was pretty convinced it's not possible... – Tomasz W Apr 22 '12 at 12:03