• Meta Class Maybe?

    From Chris Nyland@21:1/5 to All on Sat Jul 22 19:12:41 2023
    So I am stuck on a problem. I have a class which I want to use to create another class without having to go through the boiler plate of subclassing. Specifically because the subclass needs to have certain class attributes
    and I would like to control how those are passed to provide defaults and
    such. What I have working right now is

    class Foo():
    @classmethod
    def method_a(cls): print(cls.name)

    Bar = type('Bar', (Foo,), {'name': 'test1'})

    Bar.method_a()

    This is sort of fine but the user needs to know how to call type and
    include all the base classes etc.

    I could just wrap the type call in a function like below

    def BarMaker(name):
    return type('Bar', (Foo,), {'name': name})

    But then if the user needs to actually subclass Foo, to add additional
    methods that is more difficult. Of course those methods could be added as
    part of the third argument to types but that just doesn't feel very Python.

    This is all complicated by the fact that I am trying to avoid actually instancating any of these classes because their purpose is to be passed off
    to another framework to be executed. In real life most of the classes would fail to instantiate because they are relying on pieces of the framework
    that won't be up and running when this setup is happening.

    The solution I am hoping for is something like
    Foo(name, **kwds) -> Returns new class type of name
    For example
    test = Foo('test')

    So that then optionally a user could do

    class UserClass(Foo):
    def new_method():
    pass
    bar = UserClass('Bar')

    Or something similar. Since I have a working example with type I feel like there should be a way to do this with the normal class structure but I am
    at a loss for how I have messed with the __new__ and __prepare__ methods
    and I can't seem to make anything work and the examples of meta classes
    don't clearly show how to convert my type example to a class structure.

    Is what I want to do possible?

    Chris

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From risky sibam@21:1/5 to All on Sun Jul 23 06:20:18 2023
    Pada Minggu, 23 Juli 2023 pukul 06.13.23 UTC+7, Chris Nyland menulis:
    So I am stuck on a problem. I have a class which I want to use to create another class without having to go through the boiler plate of subclassing. Specifically because the subclass needs to have certain class attributes
    and I would like to control how those are passed to provide defaults and such. What I have working right now is

    class Foo():
    @classmethod
    def method_a(cls): print(cls.name)

    Bar = type('Bar', (Foo,), {'name': 'test1'})

    Bar.method_a()

    This is sort of fine but the user needs to know how to call type and
    include all the base classes etc.

    I could just wrap the type call in a function like below

    def BarMaker(name):
    return type('Bar', (Foo,), {'name': name})

    But then if the user needs to actually subclass Foo, to add additional methods that is more difficult. Of course those methods could be added as part of the third argument to types but that just doesn't feel very Python.

    This is all complicated by the fact that I am trying to avoid actually instancating any of these classes because their purpose is to be passed off to another framework to be executed. In real life most of the classes would fail to instantiate because they are relying on pieces of the framework
    that won't be up and running when this setup is happening.

    The solution I am hoping for is something like
    Foo(name, **kwds) -> Returns new class type of name
    For example
    test = Foo('test')

    So that then optionally a user could do

    class UserClass(Foo):
    def new_method():
    pass
    bar = UserClass('Bar')

    Or something similar. Since I have a working example with type I feel like there should be a way to do this with the normal class structure but I am
    at a loss for how I have messed with the __new__ and __prepare__ methods
    and I can't seem to make anything work and the examples of meta classes
    don't clearly show how to convert my type example to a class structure.

    Is what I want to do possible?

    Chris
    https://sirekan.usk.ac.id/uploads/slot4d/

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dieter Maurer@21:1/5 to Chris Nyland on Sun Jul 23 19:01:31 2023
    Chris Nyland wrote at 2023-7-22 19:12 -0400:
    So I am stuck on a problem. I have a class which I want to use to create >another class without having to go through the boiler plate of subclassing.

    Do you know about `__init_subclass__`?
    It is called whenever a class is subclassed and can be used to
    check/update the newly created class.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dom Grigonis@21:1/5 to All on Sun Jul 23 02:29:01 2023
    On 23 Jul 2023, at 02:12, Chris Nyland via Python-list <python-list@python.org> wrote:

    So I am stuck on a problem. I have a class which I want to use to create another class without having to go through the boiler plate of subclassing. Specifically because the subclass needs to have certain class attributes
    and I would like to control how those are passed to provide defaults and such. What I have working right now is

    class Foo():
    @classmethod
    def method_a(cls): print(cls.name)

    Bar = type('Bar', (Foo,), {'name': 'test1’})


    Could you write a more expressive example of what you are trying to do and what the problem actually is?

    The way you wrote the above doesn’t contain any clues why usual subclassing is not appropriate.

    By attributes you mean value attributes or methods? Why going the usual route of subclassing & controlling their behaviour via constructor arguments isn’t working?

    This is sort of fine but the user needs to know how to call type and
    include all the base classes etc.


    I don’t get what you mean by this either. Very hard to follow everything that went after, given I was lost at this point.

    Maybe it’s only me... But I if you clarified these, I might be able to help a bit.

    DG

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Dom Grigonis on Wed Jul 26 12:04:06 2023
    Dom Grigonis <dom.grigonis@gmail.com> writes:
    I don’t get what you mean by this either. Very hard to follow
    everything that went after, given I was lost at this point.

    I had the same experience. The OP should pose his question in
    the form of an executable test, e.g.,

    |I want a class T, so that this expressions evaluates to True: |T.__init__.__class__ == T.__class__

    .

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)