How do you create a class attribute that is not a standard data type?
I have one class that needs to capture an attribute set in another. However, this is not a standard data type. 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"
I need to make a reference to a screen attribute which I can set using self.screen and read in this class ... but from another class I have to set
screen = ?
under
class graphics:
What is this question mark supposed to do? I've tried 0, No "," ... nothing works, I have no idea what datatype pygame will call .: S
source to share
You rarely, if ever, refer to class attributes. You are referencing the attributes of an object.
(Also, class names must 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 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 refer to this class very often. Almost the only time a class name is used is to instantiate an object ( Graphics()
). We rarely speak of Graphics.this
or Graphics.that
to refer to the attributes of the class itself.
source to share
I think that a short explanation of the differences between class and instance attributes in Python might be helpful to you.
When you write code like this:
class Graphics:
screen_size = (1024, 768)
A class Graphics
is actually the object itself - a class object. Since you defined screen_size
inside it, screen_size
is an attribute of the object Graphics
. You can see this in the following:
assert Graphics.screen_size == (1024, 768)
In Python, these class objects can be used as functions - just use the calling syntax:
g = Graphics()
g
called an "instance" of the class Graphics
. When you instantiate a class, all attributes that do not match the attributes of the instance look at the attributes of the next class object. This is why this search works:
assert g.screen_size == (1024, 768)
If we add an attribute to an instance with the same name, however, the lookup in the instance succeeds and it does not have to look for the class object. You basically "mask" the value of the class object with the value set directly on the instance. Note that this does not 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 do in your method __init__
is exactly what we did above: setting the instance attribute 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 the definition of this class, as shown in the first statement in the above snippet.
Edit: And don't forget to check out the Python Tutorial section on classes which covers all of this and more.
source to share
ugh ... PEBKAC.
I'm so used to C that I forget that you can do more than just prototype outside of defs.
Rewritten like 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"
source to share