Python Forum
Class Attributes Inheritance
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class Attributes Inheritance
#1
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.

Please help.

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):
self.pnbr=pnbr
def cost(self):
self.cost=self.pcost*self.pnbr

Pro1=Product('Cookie','01',5)
Pro2=Product('Milk','02',3)
Reply
#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
#3
1. Init product need variables define.
class Inventory(Product):
    def __init__(self, pnbr, products)
        Product.__init__(self, "product name", 1, 20)
2. if you want a list of products do not inherit products
class Inventory:
    def __init__(self, pnbr, products)
        self.pnbr = pnbr
        self.products = products

    def cost(self, product_index):
        return self.products[product_index].pcost * self.pnbr
99 percent of computer problems exists between chair and keyboard.
Reply
#4
(Nov-16-2017, 03:56 PM)Windspar Wrote: 1. Init product need variables define.
The variables will be defined when make instance of the class.
(Nov-16-2017, 03:22 PM)Harry_Potter Wrote: 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(object):
    def __init__(self, pname, pid, pcost):
        self.pname = pname
        self.pid = pid
        self.pcost = pcost

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

    def cost(self):
        self.cost = self.pcost * self.pnbr
Use:
>>> obj = Inventory('Apple', 100, 20, 15, 'foo')
>>> obj.cost()
300
>>> obj.pcost
20
>>> obj.pnbr
15
Python 3 with super().
class Product:
    def __init__(self, pname, pid, pcost):
        self.pname = pname
        self.pid = pid
        self.pcost = pcost

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

    def cost(self):
        self.cost = self.pcost * self.pnbr
        return self.cost
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  super() and order of running method in class inheritance akbarza 7 723 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
Question [solved] Classes, assign an attributes to a class not to instances.. SpongeB0B 4 930 May-20-2023, 04:08 PM
Last Post: SpongeB0B
  Child class inheritance issue eakanathan 3 1,329 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,310 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  Distinguishing different types of class attributes Drone4four 4 2,093 Feb-21-2022, 06:34 PM
Last Post: deanhystad
  Importing issues with base class for inheritance riccardoob 5 4,674 May-19-2021, 05:18 PM
Last Post: snippsat
  Calls to Attributes of a Class SKarimi 3 3,380 Apr-22-2021, 04:18 PM
Last Post: SKarimi
  3D vector class with inheritance from 2D vector class buss0140 4 3,128 Dec-20-2020, 08:44 PM
Last Post: deanhystad
  Class inheritance oclmedyb 3 2,250 Dec-09-2020, 04:43 PM
Last Post: deanhystad
  Can we access instance variable of parent class in child class using inheritance akdube 3 13,977 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