Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I feel like this is one of the things python got right. You can start off with plain old data:

    def Foo:
        def __init__(self, foo):
            self.foo=foo
Use it in all the ways you like. If then later you discover a need for a getter/setter type setup (because you're now e.g. storing foo in a different format internally) you can switch without any of the code depending on the Foo class needing to change:

    def Foo:
        def __init__(self, foo):
            self.foo=foo

        @property
        def foo(self):
            return unmunge(self._foo)
    
        @foo.setter
        def foo(self, v):
            self._foo = munge(v)
I'll grant that the syntax is slightly wonky, but its really nice to not have to think about getter/setters when initially designing a class. You can add them when and where you need them without breaking other code.


    def __init__(self, foo):
            self.foo=foo

should, in this example, probably be:

    def __init__(self, foo):
            self.foo=munge(foo)


Ah yeah missed that. Added the munge/unmunge to it as a final pass because I figured it'd clarify the use case slightly better.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: