[Tutor] class functions/staticmethod?

James Hartley jjhartley at gmail.com
Sun Aug 11 23:58:37 EDT 2019


I am lacking in understanding of the @staticmethod property.
Explanation(s)/links might be helpful.  I have not found the descriptions
found in the Internet wild to be particularly instructive.  Given the code
below:
=====8<------------------
from collections import namedtuple

class Foo():
    Dimensions = namedtuple('Dimensions', ['height', 'width'])
    _dimensions = Dimensions(3, 4)

    def dimensions():
        print('id = {}'.format(id(Foo._dimensions)))
        return Foo._dimensions

    @staticmethod
    def dimensions1():
        print('id = {}'.format(id(_dimensions)))
        return _dimensions
=====8<------------------
The class method Foo.dimensions() is capable of accessing class members,
but Foo.dimensions1() cannot. What does the @staticmethod decorator really
add?

Thanks!


More information about the Tutor mailing list