Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
followed the feedback and gave more details. I wanted to edit becuase this answer is way better than the selected answer.
Source Link

Do the following if you want to do within init()

class Foo(object): def __init__(self): type(self).bar = property(self.get_bar) def get_bar(self): return "bar" 

The property method is basically similar to property decoration, it tells python to define a property.

And then we assign this to "bar" property of the type. This needs to be at the type level because you are defining the property for the class, so it is static, not a specific instance.

get_bar is your usual getter where you use the decorator.

Do the following if you want to do within init()

class Foo(object): def __init__(self): type(self).bar = property(self.get_bar) def get_bar(self): return "bar" 

Do the following if you want to do within init()

class Foo(object): def __init__(self): type(self).bar = property(self.get_bar) def get_bar(self): return "bar" 

The property method is basically similar to property decoration, it tells python to define a property.

And then we assign this to "bar" property of the type. This needs to be at the type level because you are defining the property for the class, so it is static, not a specific instance.

get_bar is your usual getter where you use the decorator.

Source Link

Do the following if you want to do within init()

class Foo(object): def __init__(self): type(self).bar = property(self.get_bar) def get_bar(self): return "bar"