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

18,079

Solution 1

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.

Solution 2

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$
Share:
18,079

Related videos on Youtube

Alex Gao
Author by

Alex Gao

Updated on June 26, 2022

Comments

  • Alex Gao
    Alex Gao about 2 years

    I wonder whether there is some way of using Pycharm to automatically generate getter(@property) or setter for all my variables in a class.

    If there is a way, can someone point it out ?


    thanks! Actually I meant to generate (@property) and (@x.setter) automatically.

    • Daniel Roseman
      Daniel Roseman over 9 years
      If there is, there shouldn't be. You should only be using getters and setters when you actually need them.
    • bgusach
      bgusach over 9 years
      In python you don't need to use getters and setters to keep your API safe.
    • Games Brainiac
      Games Brainiac over 9 years
      Why not just make a snippet?
  • Games Brainiac
    Games Brainiac over 9 years
    Doesn't answer the question.
  • Alex Gao
    Alex Gao over 9 years
    you can still hide some variable using Obj.__x, right ?
  • Alex Gao
    Alex Gao over 9 years
    Hi, why can't the first be achieved. I think you can still use Obj.__x to hide variable, right?
  • Matthias
    Matthias over 9 years
    You can hide the attribute with a leading __, but you can't protect access to it. Just use one(!) leading underscore if you want to make clear that the attribute is for internal use only.
  • Bartosz Marcinkowski
    Bartosz Marcinkowski over 9 years
    Prefixes like __ are only a convention and they do not prevent anything. You can get or set a variable named __x - it is not hidden.
  • Games Brainiac
    Games Brainiac over 9 years
    @Matthias Thats what I mean, you can always access stuff in Python. Everything is public.
  • Alex Gao
    Alex Gao over 9 years
    code like pastebin.com/pg85f6KY gives AttributeError: p instance has no attribute '__h'. I think that means you can not access variable starting with __.
  • Matthias
    Matthias over 9 years
    @Alex Gao: No, that just menas that you have to consider name mangling. Use print b_1._p__h and you'll get the data.
  • dav1d
    dav1d about 9 years
    Actually they do something, instead of accessing via obj.__x you have to access it via obj._Obj__x, so it is kind of hidden, but not really.
  • Mayou36
    Mayou36 over 5 years
    This really does not answer the question. Besides, there are a lot of use cases for property: e.g. if you wanna generate it on the way (like a length). The question was how to generate the property method automatically.
  • PyThagoras
    PyThagoras almost 5 years
    prop stands for @property, while s is for setter and d for deleter. thus making propsd.