Python Forum
Design Pattern for accessing subclass attributes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Design Pattern for accessing subclass attributes
#2
Quote:If a user of this class wants to determine the mfg.year
Better to store the data in a dictionary IMHO. Have the key point to a list if you want to store more than the year.
class IPhone(): 
    def __init__(self,ver): 
        self.ver = ver
        self.ver_dic={'3G': 2007,
                      '3GS':2009,
                      '4':  2010,
                      '4s': 2011,
                      '5':  2012}
        self.phone_factory() 
     
    def phone_factory(self): 
        if self.ver in self.ver_dic:
            return self.ver_dic[self.ver]
        else: 
            ## Python returns None if there is no return
            ## so this is not necessary
            return None 
Reply


Messages In This Thread
RE: Design Pattern for accessing subclass attributes - by woooee - Jul-30-2019, 10:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  which design / pattern when building classes and subclasses Phaze90 2 1,199 Nov-19-2022, 08:42 AM
Last Post: Gribouillis
  Subclass initialized property used in parent class method. Is it bad coding practice? saavedra29 5 1,973 Feb-07-2022, 07:29 PM
Last Post: saavedra29
Star Recursively convert nested dicts to dict subclass Alfalfa 1 2,984 Jan-22-2021, 05:43 AM
Last Post: buran
  use of subclass ebolisa 4 2,354 Sep-17-2020, 01:08 PM
Last Post: jefsummers
  Accessing subclass with a variable David_S 2 2,232 May-19-2020, 05:55 PM
Last Post: David_S
  How can I create a subclass of XlsxWriter? aquerci 2 2,151 May-04-2020, 07:41 PM
Last Post: aquerci
  Factory Design Pattern Prince 1 2,554 Apr-06-2018, 10:00 AM
Last Post: Larz60+
  dynamically creating a subclass sidereal 2 4,997 Jan-04-2018, 11:10 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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