Python Forum
error in constructor overriding in python3
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
error in constructor overriding in python3
#1
class Baseclass1:
    def __init__(self,x,y):
       self.x=x
       self.y=y
       print(self.x+self.y)
class Derived(Baseclass1):
    

    def cmethod(self):
        
        Baseclass1.__init__(self,x,y)#Base constructor called with class name
        print(self.x * self.y)
            
ob=Derived(4,5)
ob.cmethod()
Error:
Traceback (most recent call last): File "C:/Python37/understandingConstructor.py", line 33, in <module> ob.cmethod() File "C:/Python37/understandingConstructor.py", line 29, in cmethod Baseclass1.__init__(self,x,y)#Base constructor called with class name NameError: name 'x' is not defined
here I am not able to override construcor of the base class and getting error.probably my code is wrong...but can anyone explain how constructor overriding code in python with proper example...
Reply
#2
First of all, the error has nothing to do with overriding. The error is because x and y are not defined in cmethod. The only place they are defined is in BaseClass1.__init__, where they are defined as parameters. If you want to access the instance values of x and y from within cmethod, you need to use self.x and self.y, as you did in BaseClass1.__init__.

Second of all, you are not overriding anything. When you override a method, it has the same name. That makes the overridden version be called for the derived class. It would be more like this:

class Plus(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def act(self):
        return x + y
class Minus(Plus):
    def act(self)
        return x - y
Output:
>>> p = Plus(8, 1) >>> m = Minus(8, 1) >>> p.act() 9 >>> m.act() 7
When the Minus instance m is created, it uses the derivied __init__ method to assign x and y. But when m.act is called, it uses the overridden act method to subtract instead of adding.

So you example is a bit odd. By the time you call Derived.cmethod, the instance has already been initialized using the __init__ method inherited from BaseClass1. So it's not clear why you would call __init__ again. Also, if you are in an overridden method, and you want to call the parent class's method, you generally use super().
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 870 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  Understanding and debugging memory error crashes with python3.10.10 Arkaik 5 1,977 Apr-18-2023, 03:22 AM
Last Post: Larz60+
  Not including a constructor __init__ in the class definition... bytecrunch 3 11,520 Sep-02-2021, 04:40 AM
Last Post: deanhystad
  syntaxerror when entering a constructor MaartenRo 2 1,941 Aug-03-2020, 02:09 PM
Last Post: MaartenRo
Bug maximum recursion depth exceeded while calling a Python object error in python3 Prezess 4 3,689 Aug-02-2020, 02:21 PM
Last Post: deanhystad
  import yaml error for python3 maiya 4 13,020 Jul-15-2020, 06:07 PM
Last Post: Gribouillis
  Error in Python3.6:free() Corrupted unsorted chunks error sameer_k 2 3,798 Mar-18-2020, 09:37 AM
Last Post: sameer_k
  Error after installing Python3.8.2: "No module named 'apt_pkg'" Chromie 3 3,823 Mar-09-2020, 08:20 PM
Last Post: micseydel
  Error message when trying to install Matplotlib in Python3.8 Transitionyte 5 5,119 Jan-22-2020, 11:52 PM
Last Post: Transitionyte
  json.dumps output error in python3 prayuktibid 2 2,614 Jan-21-2020, 06:41 AM
Last Post: prayuktibid

Forum Jump:

User Panel Messages

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