Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
parent/add and child/div
#1
can i use my parent class addition answer in child class to do division and how ?
class a:
    def __init__(self,x):
        self.x=x
    def __str__(self):
        return 'addition is(%d)'%(self.x)
    def __add__(self,other):
        c=self.x+other.x
        return a(self.x+other.x)

a1=a(2)
a2=a(5)
c=a1+a2
print(c)

class b(a):
Reply
#2
Same way you would with any other method: by calling it.
class b(a):
    def spam(self, other):
        return self + other
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Chain object that have parent child relation.. SpongeB0B 10 1,076 Dec-12-2023, 01:01 PM
Last Post: Gribouillis
  Using one child class method in another child class garynewport 5 1,564 Jan-11-2023, 06:07 PM
Last Post: garynewport
  Can we access instance variable of parent class in child class using inheritance akdube 3 13,971 Nov-13-2020, 03:43 AM
Last Post: SalsaBeanDip
  Python3 binary tree not replacing parent nodes with child nodes Aspect11 0 1,763 Sep-23-2020, 02:22 PM
Last Post: Aspect11
  Getter/Setter : get parent attribute, but no Getter/Setter in parent nboweb 2 2,957 May-11-2020, 07:22 PM
Last Post: nboweb
  XML Parsing Child karthi_python 1 1,878 May-16-2019, 01:37 PM
Last Post: karthi_python
  logging: child module unable to get parent config jerryxiao 3 3,360 Apr-09-2019, 04:17 AM
Last Post: jerryxiao

Forum Jump:

User Panel Messages

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