6

I have a python class with several init variables:

class Foo(object):
    def __init__(self, d1, d2):
        self.d1 = d1
        self.d2 = d2

Is there a way to create this code automatically in PyCharm, so I don't have to type explicitly:

self.dn = dn

This pattern happens very often in my code. Is there a better (Pythonic) way to initialize classes?

I have already seen this post ( What is the best way to do automatic attribute assignment in Python, and is it a good idea?), but I don't want to use a decorator as it makes the code less readable.

4 Answers 4

6

You can start by making a custom Live Template. I'm not sure about whether you can auto-generate variable number of arguments this way, but, for two constructor arguments the live template may look like:

class $class_name$:
    def __init__(self, $arg1$, $arg2$):
        self.$arg1$ = $arg1$
        self.$arg2$ = $arg2$
$END$

This is how I've set it up in PyCharm:

enter image description here


Or, you can change the way you set the instance attributes and generalize it for N arguments, see:

Sign up to request clarification or add additional context in comments.

Comments

5

I learned of another way to solve this.

  • Move cursor to argument
  • Press alt + enter
  • Select Add field '[arg]' to class for single arg -or-
  • Press right arrow key to open sub menu
  • Select Fix all 'unused local' problems to add all args

enter image description here

2 Comments

Very handy thank you. In version 2020.2 the "Fix all" option is gone. Anyone knows how to add it back?
It looks like they may have removed it. Not sure why. I hope this isn't intentional.
1

A bit later but i think i find a solution, not sure if there is a short way but my solution is to put the args in the init like $args$ or something like that, and below the init definition where the instance variables must be put something like $argsSplit$ and in the expression of that variable put the following code:

groovyScript("def result = ''; _1.split(', ').each{ item -> result = result + 'self.' + item + ' = ' + item + System.lineSeparator() + '        '}; return result;", args)

screenshot

Comments

0

Partially answering my own question:

Very handy thank you. In version 2020.2 the "Fix all" option is gone. Anyone knows how to add it back?

I haven't found a way to add the missing option back, but a workaround that will accomplish the same, adding all missing fields to the class.

The answer comes from this post: https://stackoverflow.com/a/62060345/1067293

  • Place the mouse cursor over an unused argument and hit Alt+Enter.
  • Then hit the right arrow and select "Run inspection on..."

Run Inspection on...

  • On the new floating window select the file scope.

enter image description here

  • The inspection panel will appear on the bottom.
  • On the left select the file.
  • Then on the right, click the "Add field to class" button.

enter image description here

It's a little bit too elaborate, but nonetheless comes in handy when you've more than a few args.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.