Gary
Gary

Reputation: 2167

How to run subroutines in Jupyter Notebook?

While I recognize that this may be a trivial question, I have been having a tough time finding an answer in my research on the topic.

Let's say that I have a function, statistics that contains a variety of sub-routines, and that I put them in a cell at the top of my Jupyter Notebook.

How would I reference some of those sub-routines later on in a Jupyter Notebook? Let's say that I want to use the linearRegression sub-routinethat I created an algorithm for in my statistics function.

I am receiving the error, module 'statistics' has no attribute 'lienarRegression'

Upvotes: 0

Views: 1267

Answers (1)

Louise Davies
Louise Davies

Reputation: 15941

So from what I understand, you have a function that has functions defined inside of it and want to use these sub-functions elsewhere? You cannot do that in Python. Define the sub-functions outside the main function and it should work fine.

So, go from this:

def Foo():
    def Bar():
        print("Hello world!")
    Bar()

to this:

def Bar():
    print("Hello world!")

def Foo():
    Bar()

Upvotes: 1

Related Questions