Home > OS >  Duplicate/Copy a whole function in PyCharm
Duplicate/Copy a whole function in PyCharm

Time:01-29

is there a comfortable way to copy/duplicate a whole function in PyCharm? Of course, I can mark the whole code in the editor and copy&paste it. But is there a way to mark only the name of the function anywhere and make a copy of it? I tried to visualize it: I want to make a copy of the function breadthmergeleft and insert it in the CheckGroupSetting.py-file.

enter image description here

With kind regards

I tried to google "duplicate/copy" a whole function in PyCharm but I did not find what described above.

CodePudding user response:

if you need to use this function in multiple places create a class with the "breadthmergeleft" function then import the class and use the function or convert it to a package.

it's not a good practice to duplicate a code. always remember the "DRY" rule.

I hope this answer helps you

CodePudding user response:

For this type of operation we use 'modules', create a python file CheckGroupSetting.py and insert a function into this file. [enter image description here][1]

C:\Users\hacker\PycharmProjects\pythonProject4>ls
CheckGroupSetting.py  main.py

here we have the 'main.py' this file from which we will run our code and another file is module from which we will import function, class and etc.

CheckGroupSetting.py

def add_two_variable(x, y):
    return x   y

main.py

from CheckGroupSetting import add_two_variable

print(add_two_variable(1, 2))

it is also important to cover the topic

if '__name__' == '__main__'

if you want to understand it completely

  • Related