[Python] Modifying class variable before __new__

Discussão em 'Python' iniciado por Stack, Janeiro 29, 2025.

  1. Stack

    Stack Membro Participativo

    I'd like to modify the class variable bar of a class B so that the creation of the class A object will be performed with bar2 instead of bar1 (where bar1 is the default). This makes it easier for the user to select if they want to create class B with parameter bar1 or bar2 when using the class. I'm not opposed to other ways (eg. method) to modify it. I just want the user to be able to do it before the class is instantiated.

    Class A:

    class A(object):
    def __new__(cls, foo):
    obj = super(A, cls).__new__(cls)
    return obj


    I don't have access to Class A. Class A is coming from a third party package that doesn't have an __init__ but it takes foo as an argument of the __new__ function.

    Class B:

    class B(A):

    bar = "bar1"

    def __new__(cls, foo=bar):
    print("actual",foo)
    rm = super().__new__(cls,foo=foo)
    return rm


    Class B is a derivative class that inherits from class A. Since I want to modify the foo parameter in class A, I need to overwrite the __new__ class.

    One other piece of the puzzle is that on top of B there is a long stack of child classes that inherit from B and I don't want to modify all of their init to pass along the foo variable until class A.

    I want to run the following code (because it's easy from the user perspective to choose the value of 'bar' and because the modifications to the code are very little.

    if __name__ == '__main__':

    B.bar = 'bar2'
    b = B()
    print(b.bar)


    I get:

    actual bar1
    bar2


    whereas I'm expecting:

    actual bar2
    bar2


    I modified my example to make it work but it's not pretty and I'm seeking for a more pythonic solution. This uses bar class variable as an array and uses class methods to modify it

    class A(object):

    def __new__(cls, foo):
    obj = super(A, cls).__new__(cls)
    return obj

    class B(A):

    _bar_array = ["bar1"]

    @classmethod
    def set_bar(cls, value):
    cls._bar_array[0] = value

    @classmethod
    def get_bar(cls):
    return cls._bar_array[0]

    def __new__(cls, foo=_bar_array):
    print("actual",foo[0])
    rm = super().__new__(cls,foo=foo[0])
    return rm

    if __name__ == '__main__':

    B.set_bar("bar2")
    b = B()
    print(b.get_bar())
    print(B.get_bar())

    Continue reading...

Compartilhe esta Página