Python Forum
Accessing subclass with a variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Accessing subclass with a variable
#1
Hi All,

I'm new on this forum and pretty new in python. So maybe, my question will be quickly answered Big Grin

I'm currently trying to access a subclass with a variable.
I created a python module for class definition
module : device.py
class Device:
    def __init__(self, hostname):
        self.hostname = hostname
        self.description = None

class Router(Device):
    def __init__(self, hostname):
        super().__init__(hostname)

class Switch(Device):
    def __init__(self, hostname):
        super().__init__(hostname)
I created one class Device, and a set of subclass to inherit from the Device class. I can create objects type Router or Switch without problem.
But, as i advance with my code, i would know if it is possible to create an object (for example a Switch) depending on a variable to create the right object.

I tried this :
import device
classtype = {"rt":"Router","sw":"Switch"}
a = 'sw'
hostname = 'test'
for x in classtype:
            if x == a:
                print(f'I will create an object type ## {classtype[x]} ## with this hostname : {hostname}')
                dv_obj = device.classtype[x](hostname)
As you could see, my idea was to create the object depending on the value of "a" to avoid creating multiple if statement.

Am i crazy ? or dos it exist a solution to do this ?

Many thanks for your help !! :)
David
Reply
#2
import device

class_types = {"rt": device.Router, "sw": device.Switch}
type_strings = ('sw', 'rt', 'sw')
hostname = 'test'
for type_string in type_strings:
    print(
        f'I will create an object type ## {class_types[type_string]} ## with this hostname : {hostname}')
    dv_obj = class_types[type_string](hostname)
    print(dv_obj)
Output:
I will create an object type ## <class '__main__.Switch'> ## with this hostname : test <__main__.Switch object at 0x00000208DB7DE088> I will create an object type ## <class '__main__.Router'> ## with this hostname : test <__main__.Router object at 0x00000208DB7DE048> I will create an object type ## <class '__main__.Switch'> ## with this hostname : test <__main__.Switch object at 0x00000208DB7DE088>
Reply
#3
Many thanks for your help :)
it works fine :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Subclass initialized property used in parent class method. Is it bad coding practice? saavedra29 5 1,755 Feb-07-2022, 07:29 PM
Last Post: saavedra29
Star Recursively convert nested dicts to dict subclass Alfalfa 1 2,880 Jan-22-2021, 05:43 AM
Last Post: buran
  use of subclass ebolisa 4 2,204 Sep-17-2020, 01:08 PM
Last Post: jefsummers
  How can I create a subclass of XlsxWriter? aquerci 2 2,047 May-04-2020, 07:41 PM
Last Post: aquerci
  accessing local variable outside class priyanka08 3 2,160 Sep-24-2019, 10:00 AM
Last Post: buran
  Design Pattern for accessing subclass attributes UGuntupalli 2 2,081 Jul-30-2019, 11:09 PM
Last Post: UGuntupalli
  dynamically creating a subclass sidereal 2 4,871 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