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

5
  • 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? docs.python.org/3/library/dataclasses.html Commented Jun 8, 2020 at 7:10
  • You aren't actually creating a property object anywhere. Commented Jun 8, 2020 at 7:27
  • Possible duplicate here Commented Jun 8, 2020 at 7:30
  • @juanpa.arrivillaga thanks! I think that's the ticket. Commented Jun 9, 2020 at 0: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! Commented Jun 11, 2020 at 0:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.