0

Is there a way to incorporate Python functions from an external module/library in a function you're writing/defining, without importing them globally or beyond of the function scope?

For example, in R you can do package::function:

myfxn <- function(x){           # R example
  dplyr::as_tibble(x)
}

I'd like to do something similar in Python, but as far as I can tell, people usually just import the modules/elements of modules at the top of the file (for speed, succinctness, comprehensiveness, etc.) I know you can technically do something like this:

Example of what I know you can do in Python (but isn't what I'm looking for):

def myfxn(x):                   # Python
    from pandas import DataFrame 
    return(DataFrame(x))

...But what I'd really like is a way to use a function like the pandas DataFrame one without having to globally import it. I wish there was something like...

def myfxn(x):                   # Python
    return(pandas.DataFrame(x))

Where I wouldn't have to actually import anything. Is there any way of doing what I'm referencing? If not, is there an argument (except for a tiny amount of processing speed) about why there shouldn't be?

0 Answers0