0

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?

1
  • 1
    if FilterClass.ALPHABETIC in arguments: Commented Dec 1, 2015 at 14:02

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

Comments

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.