Skip to content
TURNKEYGNU / LINUX
AppsDocumentationBlogScreenshotsGitHubTurnKey Hub
Home/Blog/Python property gotcha

From the project

Python property gotcha

By Liraz Siri14 June 20111 min read
developmentpythongotcha

If you like using a single getter/setter function for your properties, watch out if using None for the default. If you do that you won't be able to set your property to None!

Example code and workaround...


class CantSetNone(object):
    def __init__(self):
        self._test = None

    def test(self, val=None):
        print "test(%s)" % `val`
        if val is None:
            return self._test
        else:
            self._test = val
    test = property(test, test)

class UNDEFINED:
    pass

class CanSetNone(object):
    def __init__(self):
        self._test = None

    def test(self, val=UNDEFINED):
        print "test(%s)" % `val`
        if val is UNDEFINED:
            return self._test
        else:
            self._test = val
    test = property(test, test)

Keep exploring

Find your next server

Browse ready-to-use appliances by the job you need to do.

Explore the app library
TURNKEYGNU / LINUX

Own your infrastructure without starting from scratch.

Explore TurnKey
AboutDocumentationFAQBlogGitHub
Free and open sourceSecure by defaultReady to deploy