Python: using a variable in a method in the same class

I've been doing some researching, but so far, I've come across nothing that replicates my problem fully.

Here's what I'm attempting to do:

class FilterClass:
    ALPHABETIC = "a";
    NUMERIC = "n"
    URL = "u";
    def textRestriction(self, text, arguments):
        if ALPHABETIC in arguments:
            #do crap here
        if NUMERIC in arguments:
            #do other crap here

I've created variables in class FilterClass. They are to be used outside the class, as well as the methods in the class itself, but they will never be modified.

I'm encountering that global name 'ALPHABETIC' is not defined error, and adding the variable in the method and adding a global to it does nothing. I also tried adding the __init__ method, but it did nothing as well.

Can anyone tell me where did I went wrong?

Answers


You have created class variables, so you just need to add the class name as follows:

class FilterClass:
    ALPHABETIC = "a"
    NUMERIC = "n"
    URL = "u"

    def textRestriction(self, text, arguments):
        if FilterClass.ALPHABETIC in arguments:
            print 'Found alphabetic'
        if FilterClass.NUMERIC in arguments:
            print 'Found numeric'

fc = FilterClass()
fc.textRestriction('test', 'abc n')

This would display:

Found alphabetic
Found numeric

Also, you do not need to add ; after your variables.


Instance attributes in python need to be referenced as self.identifier, not just identifier. Class attributes in python can be referenced either as self.identifier or ClassName.identifier.

So you mean:

def textRestriction(self, text, arguments):
    if FilterClass.ALPHABETIC in arguments:
        #do crap here
    if FilterClass.NUMERIC in arguments:
        #do other crap here

You created class attributes; you'll need to reference them on the class:

class FilterClass:
    ALPHABETIC = "a"
    NUMERIC = "n"
    URL = "u"

    def textRestriction(self, text, arguments):
        if FilterClass.ALPHABETIC in arguments:
            #do crap here
        if FilterClass.NUMERIC in arguments:
            #do other crap here

Class attributes are not globals, nor is the class body definition a scope as far as the methods on the class are concerned.

You could also just reference them as if they are instance attributes; Python will automatically look for class attributes if there is no such instance attribute:

def textRestriction(self, text, arguments):
    if self.ALPHABETIC in arguments:
        #do crap here
    if self.NUMERIC in arguments:
        #do other crap here

Another way of accessing these names is by querying for the current class with type(self), which would allow for subclasses to override the attributes, but ignore instance attributes:

def textRestriction(self, text, arguments):
    if type(self).ALPHABETIC in arguments:
        #do crap here
    if type(self).NUMERIC in arguments:
        #do other crap here

Need Your Help

reading a barcode generated from a weight scale in vb.net

vb.net scale barcode price

guys usually a scale's barcode contain digits that refer for the item itself and other digits which refers to the weight and price, all in the barcode itself. How can i read this barcode (getting the

Issue when I 'cout' an object

c++ class header cout

I got a issue when I cout a function. The next code is where the IDE