Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A problem with child class
#1
class Dog():
	species = 'mammal'
	
	def __init__(self, name, age):
		self.name = name
		self.age = age
		
	def description(self):
		return f'{self.name} is {self.age}'
		
	def speak(self, sound):
		return f'{self.name} says {self.age}'
	
class RussellTerrier(Dog):
	def run(self, speed):
		return f'{self.name} runs {self.speed}'
	
class Bulldog(Dog):
	def run(self, speed):
		return f'{self.name} runs {self.speed}'
	
jim = Bulldog("Jim", 12)
print(jim.description())
	
print(jim.run("slowly"))
Error:
Traceback (most recent call last): File "C:\Python36\kodovi\dogparent.py", line 25, in <module> print(jim.run("slowly")) File "C:\Python36\kodovi\dogparent.py", line 20, in run return f'{self.name} runs {self.speed}' AttributeError: 'Bulldog' object has no attribute 'speed'
I don't understand these errors:
1. Child classes have specific attributes and behaviors as well
2. Bulldog object has 'speed' within method run
Reply


Messages In This Thread
A problem with child class - by Truman - Jul-01-2018, 11:22 PM
RE: A problem with child class - by gontajones - Jul-02-2018, 12:35 AM
RE: A problem with child class - by ichabod801 - Jul-02-2018, 12:37 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  class definition and problem with a method HerrAyas 2 265 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
Question __init__ of Child Class zero_fX0 4 1,729 Mar-22-2023, 05:23 PM
Last Post: deanhystad
  Using one child class method in another child class garynewport 5 1,604 Jan-11-2023, 06:07 PM
Last Post: garynewport
  Child class inheritance issue eakanathan 3 1,343 Apr-21-2022, 12:03 PM
Last Post: deanhystad
  Can we access instance variable of parent class in child class using inheritance akdube 3 13,999 Nov-13-2020, 03:43 AM
Last Post: SalsaBeanDip
  Practice problem using lambda inside the class jagasrik 3 2,176 Sep-12-2020, 03:18 PM
Last Post: deanhystad
  calling on a method from one class into another class which is not a child NABA 5 2,823 Apr-29-2020, 07:49 PM
Last Post: deanhystad
  [split] Python Class Problem astral_travel 12 5,010 Apr-29-2020, 07:13 PM
Last Post: michael1789
  Python Class Problem JPCrosby 2 2,304 Apr-28-2020, 06:18 PM
Last Post: buran
  Class problem duckduck23 2 2,032 Feb-10-2020, 08:52 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020