Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
February 9, 2020 09:34 pm GMT

Are properties harmful?

By properties, I mean attributes with getters and setters that use the syntax of a plain attribute instead of the syntax of a function, like this:

class Test:    def __init__(self, color):        self.__color = color    @property    def color(self):        return self.__color    @color.setter    def color(self, new):        if new in ('red', 'blue', 'green', 'yellow'):            self.__color = new        else:            print("invalid color")

I don't know if properties is a universal term for them, I'm just calling them this because of the name of the Python decorator.

So I'm of the opinion that properties are bad because they are misleading, since they look like plain attributes but aren't. It's already a pattern in a lot of languages to have methods like get_someattr() and set_someattr(), and I'm not convinced there's any significant gain to using a someattr property with a getter and setter over just using the methods. I would say the only real difference is that you're defining functions and you can either make them look like functions or look like not functions. Why would you choose to make a function look like a static property? The typing increase is of minimal importance.

What's the consensus here? Am I missing some big advantages?


Original Link: https://dev.to/yujiri8/are-properties-harmful-1cak

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To