14

I am trying to make a class in Python with static variables and methods (attributes and behaviors)

import numpy

class SimpleString():    
    popSize = 1000 
    displaySize = 5
    alphatbet = "abcdefghijklmnopqrstuvwxyz "

    def __init__(self):
        pop = numpy.empty(popSize, object)
        target = getTarget()
        targetSize = len(target)

When the code runs though it says that it cannot make the array pop because popSize is not defined

4
  • 2
    Attributes must always be accessed with self, e.g., self.popSize. Commented Nov 14, 2013 at 18:44
  • or SimpleString.popSize .... when it is static class variable ... (if you access it with self changes will not reflect back to the classvariable) Commented Nov 14, 2013 at 18:48
  • 3
    you misspelled alphabet ;-) GRAMMAR NAZI ftw Commented Nov 14, 2013 at 18:53
  • Nick, join me in this chat room Commented Nov 14, 2013 at 18:59

2 Answers 2

23

You either need to access it with a self.popSize or SimpleString.popSize. When you declare a variable in a class in order for any of the instance functions to access that variable you will need to use self or the class name(in this case SimpleString) otherwise it will treat any variable in the function to be a local variable to that function.

The difference between self and SimpleString is that with self any changes you make to popSize will only be reflected within the scope of your instance, if you create another instance of SimpleString popSize will still be 1000. If you use SimpleString.popSize then any change you make to that variable will be propagated to any instance of that class.

import numpy

class SimpleString():    
    popSize = 1000 
    displaySize = 5
    alphatbet = "abcdefghijklmnopqrstuvwxyz "

    def __init__(self):
        pop = numpy.empty(self.popSize, object)
        target = getTarget()
        targetSize = len(target)
Sign up to request clarification or add additional context in comments.

Comments

3

You need to use self or the class object to access class attributes:

def __init__(self):
    pop = numpy.empty(self.popSize, object)
    target = getTarget()
    targetSize = len(target)

or

def __init__(self):
    pop = numpy.empty(SimpleString.popSize, object)
    target = getTarget()
    targetSize = len(target)

The latter form is really only needed if you want to bypass an instance attribute with the same name:

>>> class Foo(object):
...     bar = 42
...     baz = 42
...     def __init__(self):
...         self.bar = 38
...     def printBar(self):
...         print self.bar, Foo.bar
...     def printBaz(self):
...         print self.baz, Foo.baz
... 
>>> f = Foo()
>>> f.printBar()
38 42
>>> f.printBaz()
42 42

Here self.bar is an instance attribute (setting always happens on the object directly). But because there is no baz instance attribute, self.baz finds the class attribute instead.

3 Comments

you would also use the latter if you wanted to modify it in one class instance and see the change reflected in other class instances (like a static variable :P)
@JoranBeasley: the term 'static variable' is really misplaced here; Python has 'class attributes' and 'instance attributes'.
yes however class attributes behave like static variables in other languages (for the most part) ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.