Home > database >  In Python : How to use a function local Variable in different file function
In Python : How to use a function local Variable in different file function

Time:11-12

I have a function that is stored in Dates.ipynb and Second function is stored in function.ipynb file.

So in Dates.ipynb file there is function which name is input_dates() dates.ipynb Code:

import datetime as dt
from datetime import datetime

def input_dates():
    global start_date
    global exit_date
    from_date = input("enter date to start from in YYYYMMDD format :")
    end_date = input("enter date to end in YYYYMMDD format :")
    start_date = datetime.strptime(from_date, '%Y%m%d').date()
    exit_date = datetime.strptime(end_date, '%Y%m%d').date()
    
input_dates()

def year():
    global from_year
    global end_year
    from_year = start_date.year
    end_year = exit_date.year
    
year()

def month():
    global from_month
    global end_month
    from_month = start_date.month
    end_month = exit_date.month
    
month()

Function.ipynb Code:

input_dates()

def path():
    while start_date <= exit_date:
        if from_date.month<10:
            path1 = (Fu_path   "\\"   str(from_year) "\\"  str(from_month))
        else:
            path1 = (Fu_path   "\\"   str(from_year) "\\" str(from_month))
        from_date1  = relativedelta(months =  1)
        dir_list = os.listdir(path1)
        print(dir_list)

path()

Error: enter image description here

So how can I use Dates.ipynb file function in function.ipynb file funcstions.

CodePudding user response:

You can't between .ipynb files. But there are many ways you can use jupyter to save one (or both) of the files into .py (see here ).

You can then import the python file into your notebook and use the functions it contains.

CodePudding user response:

EDIT

In your case you should use the return capability of functions.

from datetime import datetime
from dateutil.relativedelta import relativedelta

FU_PATH = "foo" # You should change this to the correct path


def input_dates():
    from_date = input("Enter date to start from in YYYYMMDD format :")
    end_date = input("Enter date to end in YYYYMMDD format :")
    start_date = datetime.strptime(from_date, '%Y%m%d').date()
    exit_date = datetime.strptime(end_date, '%Y%m%d').date()
    return start_date, exit_date

start_date, exit_date = input_dates()


def path(start_date, exit_date):
    while start_date <= exit_date:
        # If-else statement is redundant, since it does the same thing
        if start_date.month < 10:
            path1 = (FU_PATH   "\\"   str(start_date.year)   "\\"   str(start_date.month))
        else:
            path1 = (FU_PATH   "\\"   str(start_date.year)   "\\"   str(start_date.month))
        start_date  = relativedelta(months =  1)
        dir_list = os.listdir(path1)
        print(dir_list)

path(start_date, exit_date)

Initial response answer

Functions don't have attributes you can access like this. This behavior you can get with classes.

For example:

class MyClass:
    my_variable = 1
    def __init__(self):
        pass
YourClass = MyClass() 
print(YourClass.my_variable)

1

Side note, try not to use global variables. It is your last resort, but in general there are always better and more elegant solutions.

  • Related