Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with self.
#1
Hello everyone!

I'm relatively new to OOP and I'm getting the error
Quote:NameError: name "self" is not defined
. I'm trying to program a seven-segment display kinda thing. Do I need all the self in the List? Without them it said, that pos is not defined...
Here's the code:
from Segment import Segment

class SevenSegments:
    margin = 20
    Segment = Segment(0, 0, 0, False)
    pos = [[self.Segment.w+(2*margin), margin, 90], 
        [self.pos[0][0] + self.Segment.h+self.margin, 2*self.margin+self.Segment.w, 0], 
        [self.pos[0][0] + self.Segment.h+self.margin, 3*self.margin+self.Segment.w+self.Segment.h, 0], 
        [self.pos[0][0], self.pos[2][1]+self.margin+self.Segment.w, 90], 
        [self.margin, 3*self.margin+self.Segment.w+self.Segment.h, 0],
        [self.margin, 2*self.margin+self.Segment.w, 0],
        [self.Segment.w +2*self.margin, self.Segment.h+self.Segment.w+2*self.margin, 90]]
    number = 0,
    def __init__(self, num):
        number = num

    def update(self):
        for i in self.self.pos:
            s = Segment(self.pos[i][0], self.pos[i][1], self.pos[i][2], True)
            s.show()
            
    
If you need to have all the files, here's the GitHub repo: https://github.com/tomXGames/SevenSegment_Display

Any help is really appreciated!

Thank you,
tomX
Reply
#2
Self refers to the current instance of the object defined by your class.

In your class definition, I am not sure when your code that is right after the class SevenSegments: will execute. Initialization code needs to be in the __init__() routine.

You also need to be careful with variable scope. pos and self.pos are not the same thing - lines 6-9. Your code does not make clear where you expect the program to get the w and h, but perhaps that is in the class definition for Segment. It is also a really bad idea to have a variable and a function with the same name - line 5. If Segment is a class as I suspect and you are endeared to that name, use lower case for the variable. Still hard to debug.

Do you mean to have a comma at the end of line 13?

The short answer to your question I believe is that in line 18 you have self.self.pos which makes no sense.
Reply
#3
Please always post entire, unmodified error trace. It contains information about the problem that greatly aids in our response.

where did you get the package Segment? I can't find it.
Reply
#4
@jefsummers Thanks for your suggestions! I implemented them and it seems no I get this error:

Quote:processing.app.SketchException: IndexError: index out of range: 0
at jycessing.mode.run.SketchRunner.convertPythonSketchError(Unknown Source)
at jycessing.mode.run.SketchRunner.lambda$startSketch$3(Unknown Source)
at java.lang.Thread.run(Thread.java:748)

(Apr-30-2020, 11:41 AM)Larz60+ Wrote: Please always post entire, unmodified error trace. It contains information about the problem that greatly aids in our response.

where did you get the package Segment? I can't find it.
@Larz60+ Sorry for not explaining the problem well enough. I now put the entire error trace in. I took the class Segment from the Segment.py file. I updated the repository, so make sure you have the new ones.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020