Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property decorator

I have a property decorator so:

def Property(f):  
    """
    Allow readable properties without voodoo.
    """
    fget, fset, fdel = f()
    fdoc = f.__doc__
    return property(fget, fset, fdel, fdoc)

Used (for example) so:

    @Property
    def method():
        """"""
        def fget(self):
            return some expression...
        return fget, None, None

So my question is about the python way of doing this. Pydev complains about

"method method should have self as first parameter"

And pylint gives me

Method has no argument

I know I can turn off this error message in pydev, but Im wondering if there is a better way to manage methods that do not take self as a parameter, something I can do better.

like image 346
jtm Avatar asked Aug 29 '26 02:08

jtm


1 Answers

You could use @staticmethod to create a method which does not receive an implicit first argument. Doesn't Python's @property decorator already do what you want?

class Foo(object):
    @property
    def bar(self):
        return 'foobar'

>>> foo = Foo()

>>> foo.bar
<<< 'foobar'
like image 115
zeekay Avatar answered Aug 31 '26 17:08

zeekay