Python Forum
Trying to create a child class in Python 2.7
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to create a child class in Python 2.7
#1
I appreciate any help - Using Python 2.7 -->
Also, it's unclear to me how to format code on this site -

Error --->

Traceback (most recent call last):
File "/Users/meldejesus/Desktop/New_stuff/Code/newApps/Python_experiments/class.py", line 28, in <module>
cat1 = Cat("Fuzz")
TypeError: __init__() takes exactly 1 argument (2 given)

I appreciate any help - Using Python 2.7 -->
Also, it's unclear to me how to format code on this site -
class Dog(object):
def __init__(self, name):
self.name = name
self.age = 6
def age_update(self, age_update):
self.age+=age_update
print("new age: " + str(self.age))
def speak (self):
print("My name is "+ self.name + "and I'm "+ str(self.age))

class Cat(Dog):
#connects it to above
def __init__(self, lives):
# connects to what it will create
super(Cat, self).__init__(name)
self.lives=CatNature()


class CatNature():
def __init__(self, lives=9):
self.lives=lives


def show_lives(self):
print("This cat has " + str(self.lives) + " lives")


cat1 = Cat("Fuzz")
print ("Cat's name is " + cat1.name + " and " + str(cat1.age))

cat1.lives.show_lives()
Traceback (most recent call last):
File "/Users/meldejesus/Desktop/New_stuff/Code/newApps/Python_experiments/class.py", line 28, in <module>
cat1 = Cat("Fuzz")
TypeError: __init__() takes exactly 1 argument (2 given)
Reply
#2
Those two snippets of code are different, and the second one shouldn't be giving that error.  So... either post the code you're running, or the error you're getting :p

Look at the definition of Cat.  In the second code block, it takes a second parameter, how many lives it has.  In the first code block, Cat didn't accept any extra parameters, which is why it was failing.

If anything, the error you should be getting would be a NameError for using a variable that wasn't defined.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How does this code create a class? Pedroski55 6 391 Apr-21-2024, 06:15 AM
Last Post: Gribouillis
Question __init__ of Child Class zero_fX0 4 1,722 Mar-22-2023, 05:23 PM
Last Post: deanhystad
  Using one child class method in another child class garynewport 5 1,596 Jan-11-2023, 06:07 PM
Last Post: garynewport
  Child class inheritance issue eakanathan 3 1,342 Apr-21-2022, 12:03 PM
Last Post: deanhystad
  Cannot convert the series to <class 'int'> when trying to create new dataframe column Mark17 3 8,531 Jan-20-2022, 05:15 PM
Last Post: deanhystad
  Create Generator in the Class quest 1 2,137 Apr-15-2021, 03:30 PM
Last Post: jefsummers
  Can we access instance variable of parent class in child class using inheritance akdube 3 13,998 Nov-13-2020, 03:43 AM
Last Post: SalsaBeanDip
  How to create and define in one line a 2D list of class objects in Python T2ioTD 1 2,043 Aug-14-2020, 12:37 PM
Last Post: Yoriz
  How to create a varying qty of instances of a Class and name them? KM007 2 2,050 May-29-2020, 12:30 PM
Last Post: KM007
  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

Forum Jump:

User Panel Messages

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