Python Forum
Class Attributes Inheritance
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class Attributes Inheritance
#2
Hi All,
I am new to programming & trying to learn python.
I have created Product class(as Parent) & Inventory (as child) class. How can i use attributes declared in product class in inventory class. In the below example i want to use Product attribute 'pcost' in inventory class.
In Cost function i want to do pcost * pnbr.

class Product():
    def __init__(self,pname,pid,pcost):
        self.pname = pname
        self.pid=pid
        self.pcost=pcost
        
class Inventory(Product):
    def __init__(self,pnbr,products):
        Product.__init__(self,pname,pid,pcost)
        self.pnbr=pnbr
    def cost(self):
        self.cost=self.pcost*self.pnbr
Reply


Messages In This Thread
Class Attributes Inheritance - by Harry_Potter - Nov-16-2017, 06:50 AM
RE: Class Attributes Inheritance - by Harry_Potter - Nov-16-2017, 03:22 PM
RE: Class Attributes Inheritance - by Windspar - Nov-16-2017, 03:56 PM
RE: Class Attributes Inheritance - by snippsat - Nov-16-2017, 07:01 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  super() and order of running method in class inheritance akbarza 7 1,056 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
Question [solved] Classes, assign an attributes to a class not to instances.. SpongeB0B 4 1,127 May-20-2023, 04:08 PM
Last Post: SpongeB0B
  Child class inheritance issue eakanathan 3 1,510 Apr-21-2022, 12:03 PM
Last Post: deanhystad
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 1,436 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  Distinguishing different types of class attributes Drone4four 4 2,335 Feb-21-2022, 06:34 PM
Last Post: deanhystad
  Importing issues with base class for inheritance riccardoob 5 5,005 May-19-2021, 05:18 PM
Last Post: snippsat
  Calls to Attributes of a Class SKarimi 3 3,600 Apr-22-2021, 04:18 PM
Last Post: SKarimi
  3D vector class with inheritance from 2D vector class buss0140 4 3,355 Dec-20-2020, 08:44 PM
Last Post: deanhystad
  Class inheritance oclmedyb 3 2,408 Dec-09-2020, 04:43 PM
Last Post: deanhystad
  Can we access instance variable of parent class in child class using inheritance akdube 3 14,214 Nov-13-2020, 03:43 AM
Last Post: SalsaBeanDip

Forum Jump:

User Panel Messages

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