Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
User Subclasses
#3
Thanks for the feedback, appreciated.

Yea I guess I wasn't descriptive enough on my classes. But you are right, I'm not directly using the base class, I don't think I'll ever need to directly use it. Was just housing common methods that are used in all the sub-classes. Felt like it would be redundant to add them to the sub classes. Maybe this isn't correct thing to do?

Issue is that in my project, I don't want the child class instances to be touched by the end-user. They have predefined instance names that must remain the same. My example in my original post wasn't very accurate to what I'm actually doing, was trying to simplify it too much.

Here is a better representation, still simplified though:
# File: hierarchy.py
# User should not touch this file..

class BASE(object):

    def method(self):
       print("My value is %s" % self.value)

class SubHier(BASE):

    def __init__(self, value):
        self.value = value


class Top(BASE):

    def __init__(self, name):
        self.name = name

    def build(self):
        self.HIER1 = SubHier(1)
        self.HIER2 = SubHier(2)

TOP_INST = Top("top_hier")
TOP_INST.build()
Anyways, I wanted the TOP/SubHier classes to be hidden from the user, and the user can only access the pre-generated instances. I'm treating this as more of a data structure that can be accessed (kinda like a dictionary with methods to access it). So the Top and SubHier classes are more containers for the data and the base class provides different functions to access that data in different ways.

So the user code (in separate file) would just import the hierarchy, and access it through the base class methods:

# File: user.py

from hierarchy import TOP_INST

# Access hierarchy...
TOP_INST.HIER1.method()
TOP_INST.HIER2.method()
I wanted to have some hook to let the user edit or extend the 'access' methods in the BASE class, without needing to change how the hierarchy classes are instantiated. That's why I was considering extending the BASE class.

Thanks!
Reply


Messages In This Thread
User Subclasses - by holyghost - Mar-16-2021, 08:10 PM
RE: User Subclasses - by buran - Mar-16-2021, 08:47 PM
RE: User Subclasses - by holyghost - Mar-16-2021, 11:40 PM
RE: User Subclasses - by deanhystad - Mar-17-2021, 03:36 AM
RE: User Subclasses - by buran - Mar-17-2021, 12:03 PM
RE: User Subclasses - by deanhystad - Mar-17-2021, 12:18 PM
RE: User Subclasses - by buran - Mar-17-2021, 12:33 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  "unexpected keyword arg" when initializing my subclasses Phaze90 3 3,234 Nov-25-2022, 07:39 PM
Last Post: Gribouillis
  which design / pattern when building classes and subclasses Phaze90 2 1,145 Nov-19-2022, 08:42 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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