Hi ,
I cant figure out what is wrong with my code .Why I am getting the attribution error .Please , see attached photo .
Thank in advance for your help
HJ
Let’s breakdown the error message as it’s very helpful.
int not having an attribute num_surfboards means at some point your program tried to use an int and get a num_surfboards attribute from it.
x = 4
print(x.num_surfboards) # will throw an attribute error
moreover it tells you the exact place the error was triggered, line 16 in surfboard.py (which is not in the screenshot, so we have no more information).
It’s very possible the parameters got mixed up in the function definition but without seeing the other code it’s hard to be more specific.
Hi ,
Thank you for your explanation
I assumed that whoever replies to my post would have an access to the that project which is in the intermediate python course in the unit test section .Any way I included a screen shot of that file
Thank again for your help
Oh yea, not all users here are users of the product.
Yea this is what I though it was. When you wrote your test, you are using your class method in a static way (you should look this up for reference later). Loosely, static methods can be called without instantiating functions.
For example if I write
class Something:
def __init__(self):
self.foo = 0
def bar(self, num=1):
print(self.foo + num)
Something.bar(2)
I will get the same error you did about int not having an attribute foo. That is because the 2 I pass through bar(2) is binding to the self variable. This is interesting.
Let’s say I just call Something.bar(), then I get TypeError: Something.bar() missing 1 required positional argument: 'self'
. This really confirms that it expects an argument… self! So here we remember that in class methods, we write self in class methods to implicitly pass to the function the instance of that function.
For example
joe = Person()
jack = Person()
joe.setName("Joe")
jack.setName("Jack")
here, whatever the method setName() does, it must know that the objects the variables joe and jack point to are different. This is because we want the freedom if we have a static method to sometimes call a method with no effects on self.
So how do we fix this? We instantiate the Something class
class Something:
def __init__(self):
self.foo = 0
def bar(self, num=1):
print(self.foo + num)
my_instantiated_something = Something()
my_instantiated_something.bar(2) # will do as wanted
now you can adapt this as you see fit.
References:
This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.