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.
I tried:
Is there a way to solve my problem or do I have to outsource the "solid" variable?
Kind regards
You can assign it to the class member after the class definition.
class RoadMark:
...
RoadMark.solid = RoadMark()
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(...)
It seems issue is indentation.
You're trying to create object of a class even before that class is execute/created.
So we need to create object after class is executed something like:
class Road:
...
solid = Road()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.