0

I want to create python class with read only properties.

Please see this example:

class ClassProperty(object):
    def __init__(self, getter):
        self.getter = getter

    def __get__(self, instance, owner):
        return self.getter(owner)


class Constants(object):
    @ClassProperty
    def version(cls):
        return '1.0.11'

So under this (cls) word i have this message:

Usually first parameter of method is named self

So i wonder is i need to declare it this way:

class Constants(object):
    @ClassProperty
    def version(self):
        return '1.0.11'

And in this way the message disappear

Barmar
  • 741,623
  • 53
  • 500
  • 612
falukky
  • 1,099
  • 2
  • 14
  • 34
  • Possible duplicate of [Python read-only property](https://stackoverflow.com/questions/14594120/python-read-only-property) – Malekai May 02 '19 at 07:58
  • `cls` is normally only used in metaclass methods. Ordinary class methods use `self`. These are just naming conventions, you're not even using the parameter, so it doesn't really matter. But your IDE is helping you follow normal practices. – Barmar May 02 '19 at 07:58
  • 1
    The message is just a hint, it doesn't affect how the methods work. – Barmar May 02 '19 at 07:58
  • Possible duplicate of [How to create a read-only class property in Python?](https://stackoverflow.com/questions/3203286/how-to-create-a-read-only-class-property-in-python/26634248) – Malekai May 02 '19 at 07:59
  • 1
    @LogicalBranch What do any of those questions have to do with the message "Usually first parameter of method is named self"? – Barmar May 02 '19 at 07:59
  • So this is ok to change it to self ? – falukky May 02 '19 at 08:00
  • You can call it whatever you want. It's not being used, so it doesn't matter. – Barmar May 02 '19 at 08:00
  • Function parameters can be named anything, they don't have any special meaning. LIke I said, it's just a convention. – Barmar May 02 '19 at 08:01

0 Answers0