I have a class which contains a large amount of properties. More specifically, the class represents my router.
I want to divide its properties to categories, meaning that in order to retrieve the LAN ip and the WAN ip I would not have to type:
router.wan_ip
router.lan_ip
But instead type:
router.wan.ip
router.lan.ip
The properties are dynamic and retrieved when calling their functions. My current implementation:
class Category(object):
def __init__(self, parent):
self._parent = parent
class Lan(Category):
@property
def ip(self):
self._parent._get_property("lanip")
class Wan(Category):
@property
def ip(self):
self._parent._get_property("wanip")
class Router(object):
def __init__(self, ):
self.lan = Lan(self)
self.wan = Wan(self)
def _get_property(self, property_name):
# Some code here
But I wounder if there is a better way
You are probably overthinking this structure, but if you want objects…
WanandLanshould not exist. You can pass a prefix for theCategoryclass so it will search for...ip. The_get_propertymethod could be replaced by a dictionary. If you need to calculate the values on the fly, you may create adictwith functions to be called.By the way, this program doesn’t need to have any class at all. Seems like you just need dictionaries. You can create a function to build these dictionaries if needed