Question

I have one class that needs to grab an attribute that is set in another. It's not a standard data type though. Here's the code;

class graphics:
def __init__(self, Fullscreen = False, Width = 640, Height = 480):
    print "Graphics Init"
    SCREEN_SIZE = (Width, Height)
    pygame.init()
    if Fullscreen:
        self.screen = pygame.display.set_mode(SCREEN_SIZE, FULLSCREEN, 32)
        print "Fullscreen Initialized"
    else:
        self.screen = pygame.display.set_mode(SCREEN_SIZE, 0, 32)
        print "Non-Fullscreen Initialized"

What I need to do is reference the screen attribute, which I can set with self.screen and be readable within that class...but from another class I have to set

screen = ?

under

class graphics:

What does that question mark need to be? I've tried 0, None, ""...nothing seems to work, I have no idea what data type that pygame call would be. :S

Was it helpful?

Solution

You rarely, if ever, reference attributes of a class. You reference attributes of an object.

(Also, class names should be uppercase: Graphics).

class Graphics:
SCREEN_SIZE = (640, 480)
def __init__(self, Fullscreen = False, Width = 640, Height = 480):
    print "Graphics Init"
    self.SCREEN_SIZE = (Width, Height)
    pygame.init()
    if Fullscreen:
        self.screen = pygame.display.set_mode(SCREEN_SIZE, FULLSCREEN, 32)
        print "Fullscreen Initialized"
    else:
        self.screen = pygame.display.set_mode(SCREEN_SIZE, 0, 32)
        print "Non-Fullscreen Initialized"

Here's an example of getting or setting an attribute of an object.

g= Graphics() # create an object
# access the object's instance variables
print "screen", g.screen
g.screen= pygame.display.set_mode(SCREEN_SIZE, FULLSCREEN, 32)

Note that we created an object (g) from our class (Graphics). We don't reference the class very often at all. Almost the only time a class name is used is to create object instances (Graphics()). We rarely say Graphics.this or Graphics.that to refer to attributes of the class itself.

OTHER TIPS

I'm thinking that a short explanation of the difference between class and instance attributes in Python might be helpful to you.

When you write code like so:

class Graphics:
    screen_size = (1024, 768)

The class Graphics is actually an object itself -- a class object. Because you defined screen_size inside of it, screen_size is an attribute of the Graphics object. You can see this in the following:

assert Graphics.screen_size == (1024, 768)

In Python, these class objects can be used like functions -- just use the invocation syntax:

g = Graphics()

g is called an "instance" of the class Graphics. When you create instances of a class, all attribute lookups that don't match attributes of the instance look at the attributes of the class object next. That's why this lookup works:

assert g.screen_size == (1024, 768)

If we add an attribute to the instance with the same name, however, the lookup on the instance will succeed, and it won't have to go looking to the class object. You basically "mask" the class object's value with a value set directly on the instance. Note that this doesn't change the value of the attribute in the class object:

g.screen_size = (1400, 1050)
assert g.screen_size == (1400, 1050)
assert Graphics.screen_size == (1024, 768)

So, what you're doing in your __init__ method is exactly what we did above: setting an attribute of the instance, self.

class Graphics:

    screen_size = (1024, 768)

    def __init__(self):
        self.screen_size = (1400, 1050)

g = Graphics()
assert Graphics.screen_size == (1024, 768)
assert g.screen_size == (1400, 1050)

The value Graphics.screen_size can be used anywhere after this class definition, as shown with the first assert statement in the above snippet.

Edit: And don't forget to check out the Python Tutorial's section on classes, which covers all this and more.

It's likely an object type so

self.screen = object()

might work (If I understood your question correctly).

ugh...PEBKAC.

I'm so used to C that I keep forgetting you can do more than just prototype outside of defs.

Rewrote as this and it worked:

class graphics:
SCREEN_SIZE = (640, 480)
screen = pygame.display.set_mode(SCREEN_SIZE, 0, 32)
def __init__(self, Fullscreen = False, Width = 640, Height = 480):
    print "Graphics Init"
    self.SCREEN_SIZE = (Width, Height)
    pygame.init()
    if Fullscreen:
        self.screen = pygame.display.set_mode(SCREEN_SIZE, FULLSCREEN, 32)
        print "Fullscreen Initialized"
    else:
        self.screen = pygame.display.set_mode(SCREEN_SIZE, 0, 32)
        print "Non-Fullscreen Initialized"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top