testing existing attribute of a @classmethod function, yields AttributeError

Posted by alex on Stack Overflow See other posts from Stack Overflow or by alex
Published on 2010-05-07T21:19:47Z Indexed on 2010/05/08 12:28 UTC
Read the original article Hit count: 147

Filed under:
|

i have a function which is a class method, and i want to test a attribute of the class which may or may not be None, but will exist always.

class classA():
 def __init__(self, var1, var2 = None):
  self.attribute1 = var1
  self.attribute2 = var2

 @classmethod
 def func(self,x):
  if self.attribute2 is None:
   do something  

i get the error

AttributeError: class classA has no attribute 'attributeB'

when i access the attribute like i showed but if on command line i can see it works,

x = classA()
x.attributeB is None 
True

so the test works.

if i remove the @classmethod decorator from func, the problem disapears.
if i leave the @classmethod decorator, it only seems to affect variables which are supplied default values in the super-class's constructor.

whats going on in the above code?

© Stack Overflow or respective owner

Related posts about python

Related posts about class