Pycharm (@property) and (@x.setter) autogeneration

There isn't a way to do so. You can extract certain parts of code, but you cannot have pycharm generate getters and setters for you. There is no need either, since all variables are public, and the _var values, even though are treated as private variables can be altered as well.

EDIT (Change in question):

If you want to write less code when making getters and setters, then just use the props or the propsdsnippet in PyCharm.


Just because Python doesn't enforce private members doesn't mean you have to do without them. But be warned that getters and setters are function calls and are much slower than direct member access.

PyCharm doesn't offer a menu to create properties for all your private members but it has a live template for it: within your class (indentation must be correct) type prop and press enter. A stub of your read only property will appear.

I adopted my live template to easily generate a getter like property: Open Settings (Ctrl+Alt+S) -> Editor -> Live Templates -> Python -> prop

@property
def $NAME$(self):
    return self.__$NAME$

The same for live template props for read write access:

@property
def $NAME$(self) -> $TYPE$:
    return self.__$NAME$

@$NAME$.setter
def $NAME$(self, $NAME$: $TYPE$):
    self.__$NAME$ = $NAME$

Tags:

Python

Pycharm