Typeerror - Classes In Python
Solution 1:
You assign a channel
attribute in your __init__
:
self.channel = 1
This shadows the channel()
method on the class. Rename the attribute or the method.
Attributes on the instance trump those on the class (except for data descriptors; think property
s). From the Class definitions documentation:
Variables defined in the class definition are class attributes; they are shared by instances. Instance attributes can be set in a method with
self.name = value
. Both class and instance attributes are accessible through the notation “self.name
”, and an instance attribute hides a class attribute with the same name when accessed in this way.
Your methods also expect a parameter that you are not passing in in your example, but I'm figuring you'll solve that yourself next.
Solution 2:
You have a method called channel
and a variable called channel
. The variable, which is an int
, shadows the method (steals it's name so that you can't access it). Rename wither the method or the variable and this will solve your problem.
Solution 3:
Actually your code after the Edit exposed the problem Martijn was already indicating at. Your self.change()
expects a parameter which you don't provide in myTele.change()
. Since you are not using the parameter in the method, you should define change
as:
defchange(self):
self.channel = input('Pick a channel: ')
print('You are on channel ' + self.channel)
volume_up
and volume_down
actually assign a string to self.amount instead of calling a function. You probably want to change those to
defvolume_up(self, amount):
self.amount = input('Increase the volume by: ')
self.volume += self.amount
print('The volume is now ' + self.volume)
defvolume_down(self, amount):
self.amount = input('Decrease the volume by: ')
self.volume -= self.amount
print('The volume is now ' + self.volume)
Since you are setting self.amount
every time before using it, you can probably just make it a local variable to the method ( amount
). If you have future plans to have a method xyz()
that uses self.amount
without first setting it, you should make sure self.amount
is set in __init__()
to a reasonable value, in case xyz()
is called before the volume changing methods.
Post a Comment for "Typeerror - Classes In Python"