0

I some questions about some code I've been looking at. What does the @staticmethod and @property mean when it is written above a method definition in Python like the following?

@staticmethod 
def methodName(parameter):
    Class_Name.CONSTANT_VARIABLE = parameter

@property
def methodName(parameter):
    Class_Name.CONSTANT_VARIABLE = parameter
jamylak
  • 128,818
  • 30
  • 231
  • 230
letter Q
  • 14,735
  • 33
  • 79
  • 118
  • 7
    Docs? [staticmethod](http://docs.python.org/release/3.1.5/library/functions.html#staticmethod) , [property](http://docs.python.org/release/3.1.5/library/functions.html#property) – jdi Jul 02 '12 at 04:56
  • 2
    Check out [link](http://stackoverflow.com/questions/739654/understanding-python-decorators) – Matthew Adams Jul 02 '12 at 05:10

3 Answers3

2

The decorator syntax is shorthand for this pattern.

def methodName(parameter):
    Class_Name.CONSTANT_VARIABLE = parameter
methodName = some_decorator(methodName)

can be rearranged like this

@some_decorator
def methodName(parameter):
    Class_Name.CONSTANT_VARIABLE = parameter

One advantage is that it sits at the top of the function, so it is clear that it is a decorated function

Are you also asking what staticmethods and properties are?

John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • Thank you very much for the help but I guess I am also asking what staticmethods and properties are. – letter Q Jul 02 '12 at 06:28
1

There is a sample code

class Class1(object):
    def __init__(self):
        self.__x = None

#       you can call this method without instance of a class like Class1.method1()
    @staticmethod
    def method1():
        return "Static method"

    def method2(self):
        return "Class method"

    @property
    def x(self):
        print "In getter"
        return self.__x

    @x.setter
    def x(self, value):
        print "In Setter"
        self.__x = value
Pooya
  • 4,385
  • 6
  • 45
  • 73
0

A staticmethod is just a function that has been included in a class definition. Unlike regular methods, it will not have a self argument.

A property is a method that gets run upon attribute lookup. The principal purpose of a property to is support attribute lookup but actually run code as if a method call had been made.

Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
  • Thank you very much Raymond. To clarify, is one of the reasons to use a staticmethod possibly to change a class variable when the staticmethod of the class is invoked? Also, what do you mean by "A property is a method that gets run upon attribute lookup?" I've only been learning how to program for the past 6 months and have never heard about this. Thanks for the help in advance! – letter Q Jul 02 '12 at 06:41
  • Static methods are used for functionality that doesn't need access to either an instance or the class itself (that is why there is no *self* or *class* in the parameter list for a static method). Accordingly, static methods are not used to change class variables (they don't even know about the class they are in). – Raymond Hettinger Jul 02 '12 at 08:03
  • Then in what way would you actually use a static method? For example, could you use it to change a class CONSTANT? Thanks! – letter Q Jul 02 '12 at 18:19