Pythons's Attributeerror: 'nonetype' Object Has No Attribute 'errors'
Solution 1:
Your use of returns
and errors
as class variables doesn't seem like a good idea. There is only one instance of each of those variables, no matter how many instances of Nollty
you create. Instead, do this:
def__init__(self, filename, database):
self.returns = 0self.errors = 0# rest of __init__
Next, the use of returns
to indicate a return value doesn't seem like a good idea either. In Python, one would normally raise an exception to indicate a problem in the constructor. That way, the caller can't simply ignore a problem by forgetting to check returns
.
Similarly, use an exception from select()
to indicate a problem with the parameter. My recommendation would be to eliminate both returns
and errors
.
You're also not returning any value at all from select
, so that's why query
ends up being None
(in Python, None
is a special value). You either want to return something useful from select()
, or don't assign its result to anything if it doesn't return a useful value.
Solution 2:
select()
doesn't return an explicit value, so it has a NoneType
return value. Change your code so that select()
will return either 1 or 0, depending on the success or failure of your code.
Solution 3:
It looks like select() is not returning a value, so it defaults to a NoneType (null). Alternatively (what you probably meant to do), change query to nollty.
Solution 4:
The errors variable in your select method is a local variable. You need to explicity set the class errors variable. Also, as others suggested your select method has no return statement. From your code it looks like you are trying to access the errors variable of nollty, but you are checking for it in query variable which is logically a None.
You have two options depending on what your objective is. Return the errors from select or set self.errors = errors before your select method returns. Or do both if you like, as below. Usually you'd probably return False if the select failed and True if it succeeded, but it's not required.
class Nollty:
...
def select(self,table):
...
self.errors = errors
return errors
errs = nollty.select("aaa_auto")
if nollty.errors == 0:
print"Successfully chosen the table!"
##functionally equivalent
#if errs==0:
# print"Successfully chosen the table!"
Post a Comment for "Pythons's Attributeerror: 'nonetype' Object Has No Attribute 'errors'"