0

Basically, here's the format of my code:

class SomeClass():

    # RegEx to remove all non-alphanumeric characters from a string
    def alphaNum(original):
        return str(re.sub(r'[^a-zA-Z0-9]','', original))


    # Write to xlsx file =====================================================
    def write(self):
        #CODE###
        uglyString = 'asasdf-)aws'

        print alphaNum(uglyString)
        #I've also tried calling self.alphaNum(uglyString), for what it's worth

and I get "global name 'alphaNum' is not defined" when I call write from another file (details left out, but I know the print statement is where the error occurs)

I'm positive I'm overlooking something dumb, I (like to think that I) have a good handle on scope, defining things before using them, etc.

edit:

Thanks for the help guys! I ended up just moving alphaNum() outside of the class. And for those interested, the actual purpose of this is dealing with the quirks of Amazon's boto interface for CloudFormation. It'll happily return asset id values with '-' in them, then complain that you can't have any in a template. Such is life...

Chris43234
  • 38
  • 4

1 Answers1

4

That's because alphaNum is a member of SomeClass. Further, it's not a staticmethod, so the first parameter should be self.

I'm not really sure why you're putting all this in a class, but it should look like this instead:

class SomeClass():

    @staticmethod
    def alphaNum(original):
        """RegEx to remove all non-alphanumeric characters from a string"""
        return str(re.sub(r'[^a-zA-Z0-9]','', original))

    def write(self):
        """Write to xlsx file"""

        uglyString = 'asasdf-)aws'

        print SomeClass.alphaNum(uglyString)
Mike DeSimone
  • 41,631
  • 10
  • 72
  • 96
  • Definitely seems like ```alphaNum``` could be defined outside the class. – wwii Nov 14 '14 at 16:26
  • I'm assuming it's a namespace thing. – Mike DeSimone Nov 14 '14 at 16:27
  • So does Python not let you reference sibling functions? I think I understand what you mean, but I'm not quite sure how to fix my code. Just rip alphaNum out of the class completely? – Chris43234 Nov 14 '14 at 16:27
  • Sure it does, but you need to specify the scoping. When looking for a symbol, it starts with the current function's namespace, then moves to the module's namespace. It doesn't walk up the subclass tree like C++ does. So to specify a static member function, you just need to either put the class name in front (e.g. `SomeClass.alphaNum`) or an object name (e.g. `self.alphaNum`). The latter method didn't work for you before because `alphaNum` wasn't marked as `staticmethod` so it was being called as `SomeClass.alphaNum(self, uglyString)` which doesn't match the param list. – Mike DeSimone Nov 14 '14 at 16:30
  • P.S. [Please don't parse XML with regular expressions.](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – Mike DeSimone Nov 14 '14 at 16:36
  • 1
    @Chris43234 - Yes, `alphaNum` should be placed outside the class IMO. It operates independently of the class and is also not specifically related to what the class does. It is just a helper function. Generally speaking, the only functions that should be methods of a class are 1) those that alter state or 2) those that are so closely related to the class that making them stand-alone functions is impractical. –  Nov 14 '14 at 16:39