0

I'm trying to create a dynamic class in python that also has dynamic properties; but I'm struggling with this.

Here's a simple non-working sample:

class baseA(object):

    def __init__(self):
        self._a_info = 1

    def dump(self, pref=""):
        print("%s %d" % (pref, self._a_info))


def b_init(self, parent_class):
    parent_class.__init__(self)


def _dump(self, pref=""):
    print("%s: %d" % self._b_info)


attrs = {"__init__": b_init,
         "dump": lambda self, pref: _dump(self, pref=pref)}

for field in ["field1", "field2"]:
    attrs["_%s" % field] = field
    attrs[field] = lambda self: getattr(self, "_%s" % f)

tmpb = type("baseB",
            (baseA, ),
            attrs)

t = tmpb()
t.dump(pref="Field: ")

Obviously, the above doesn't work. For one thing print(t.field1) will print an unbounded method warning, since attrs[prop] is a function and not a value. (I was hoping to simulate what @property does to methods).

What I'm trying to do is to create a class dynamically while setting properties for it. That said, I now realize that "attrs[prop] = lambda self: getattr(self, "_%s" % prop) is wrong as that makes attrs[prop] a function.

Is it even possible to use the type() function to create a dynamic class that has the following property getter/setters?

So like converting the following:

class baseB(baseA):

    def __init__(self):
        self._field1 = "field1"
        self._field2 = "field2"
        self._field3 = "field3"

    @property
    def field1(self):
        return self._field1

    @field1.setter
    def field1(self, in_val):
        self._field1 = in_val

    @property
    def field2(self):
        return self._field2

    @field2.setter
    def field2(self, in_val):
        self._field2 = in_val

    @property
    def field3(self):
        return self._field3

    @field3.setter
    def field3(self, in_val):
        self._field3 = in_val

to

   type("baseB",
        (baseA, ),
        someattributes_dictionary)

?

If it was a one off script, then sure I'd just do the long way; but if I need to dynamically create different classes with different properties, the typical 'class ...' will be tedious and cumbersome.

Any clarifications appreciated,

Ed.

--

[1] - https://www.python-course.eu/python3_classes_and_type.php

ewokx
  • 2,204
  • 3
  • 14
  • 27
  • If your properties are all read/write properties with both a getter and a setter that does nothing else, why not simply use a data class? https://docs.python.org/3/library/dataclasses.html – Grismar Jun 08 '20 at 07:10
  • You aren't actually creating a `property` object anywhere. – juanpa.arrivillaga Jun 08 '20 at 07:27
  • Possible duplicate [here](https://stackoverflow.com/questions/57811102/creating-class-properties-dynamically/57811222#57811222) – juanpa.arrivillaga Jun 08 '20 at 07:30
  • @juanpa.arrivillaga thanks! I think that's the ticket. – ewokx Jun 09 '20 at 00:22
  • After fiddling and fudging the code a bit, I ended up just forgoing the idea of dynamic classes w/ dynamic properties/methods. It was getting way too confusing and complicated for my taste. Thanks @juanpa.arrivillaga for the help. Appreciate it alot! – ewokx Jun 11 '20 at 00:42

0 Answers0