[SOLVED] Editing Property Functon of a recursive Property

Issue

This Content is from Stack Overflow. Question asked by cfvWEW

I have a function which functionality can be described with this code:

class Test:
    def __init__(self) -> None:
        pass

    def __str__(self) -> str:
        return "testing"

    @property
    def first(self):
        return Test()


test = Test()
print(test.first)
print(test.first.first)

(Output)

testing
testing

Basicly, it has a recursive property.
I want to edit the property function of the class, but save the recursion:
(This doesnt work but explains my idea)

def edit_property(test_class):

    @test_class.property
    def first(test_class):
        return test_class.first + " test"

edit_property(test)

The result of this code would be (if it would work)

testing test
testing test test



Solution

class Test:
    def __init__(self,value='') -> None:
        self.value = value

    def __str__(self) -> str:
        return "testing" + self.value

    @property
    def first(self):
        return Test(self.value + ' test')


test = Test()
print(test.first)
print(test.first.first.first)


This Question was asked in StackOverflow by cfvWEW and Answered by islam abdelmoumen It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?