[Tutor] @property vs @classmethod

Evuraan evuraan at gmail.com
Sat Jul 8 19:03:24 EDT 2017


Greetings!

I was hoping to learn when to use classmethod and when to use property.
They both seem similar (to me at least..), what's the pythonic way to
choose between them?

$ cat ./try.py
#!/usr/bin/python3

class _someclass:
    _tag = "sometag"

    def __init__(self,_text):
        self._text = _text
        print("__init__ says {}".format(self._text) )
        self.class_method_thingy()
        self.class_property_thingy

    @property
    def class_property_thingy(self):
        """class property thing"""
        _txt_cp = "Hello from class property thingy"
        self.a = _txt_cp

    @classmethod
    def class_method_thingy(self):
        """class method thingy"""
        _txt_cm = "Hello from class method thingy"
        self.b = _txt_cm

class _subclass(_someclass):
    pass


if __name__ == "__main__":
    blah = _someclass("input")
    print(blah.a)
    print(blah.b)

$ ./try.py
__init__ says input
Hello from class property thingy
Hello from class method thingy


Thanks!


More information about the Tutor mailing list