0

I am very confused that I did not directly found a topic about my concern:

I got the class RoadMark. In this class, I wanna create a member that is a specific instance of that class. But I am not able to call the constructor.enter image description here

I tried:

  • calling the init function (but it has no return, so its worthless)
  • define the member as a function with the @property attribute (but these returns cannot have members, so worthless for an object)
  • Trying "solid = self.RoadMark..." (also not working)
  • Using a factory function to create the object (but in the method, RoadMark is also not defined)

Is there a way to solve my problem or do I have to outsource the "solid" variable?

Kind regards

2 Answers2

5

You can assign it to the class member after the class definition.

class RoadMark:
    ...
RoadMark.solid = RoadMark()
Chao Peng
  • 149
  • 7
1

Python code is executed as it is encountered. When the interpreter encounters the line class RoaMark:, it starts creating a new class object. Then it starts to run the code inside the class body to determine the attributes of the class. When the code in the body is running, the class object does not exist yet, so you can't access it. What you can do is wait until the class is created before assigning an attribute to it:

class RoaMark:
   ...

RoaMark.solid = RoaMark(...)
Mad Physicist
  • 76,709
  • 19
  • 122
  • 186