1

Novice question: I have currently written a Python script and in the middle I define a class which I use. Nevertheless, I understand I can have the class saved separately so as to call it again and again in different scripts with no need to define it all the time.

The class reads:

class hello

    """
    blah
    
    """

    def __init__(self, blah)

return self

If I am in the directory dir, and I have saved a Jupyter notebook my_notebook.ipynb should the class be saved in the same directory as my_class.py? And how do I call it in the notebook afterwards?

Marion
  • 271
  • 3
  • 11
  • Do research on how imports work: https://chrisyeh96.github.io/2017/08/08/definitive-guide-python-imports.html For your situation, `from my_class import hello` should work. Also check https://stackoverflow.com/a/52923466/6402099 – emremrah Aug 06 '21 at 08:26

1 Answers1

1

Save the file in same directory and use import in your .py files. Like if you have class saved in a file called classes.py then you must write import classes in your other .py file. And they must be in the same directory.

Hope that answers your question.

AaYan Yasin
  • 566
  • 3
  • 15
  • If incase it don't work then feel free to ask me again and if it works then mark it as correct answer so others can see that it is working. ```Happy Coding``` – AaYan Yasin Aug 06 '21 at 09:30