英文:
Importing a class with its dependencies
问题
我正在尝试从另一个文件中导入我创建的类到我的当前工作文件中。在这个示例中,该类如下:
ClassFile
class example_class:
def __init__(self, ticker):
self.df = yf.download(ticker, start='2020-12-31')
def do_something(self):
return something
然后,当我尝试将其导入我的工作文件并运行代码时:
WorkingFile
import ClassFile
import yfinance as yf
instance = example_class('MSFT')
我遇到了以下错误:
name 'yf' is not defined
我理解yfinance
没有被导入某个地方,但在研究后,我没有能够理解如何导入它。
基本上,我正在尝试理解如何从另一个文件中导入一个类以及该类中的依赖项。
英文:
I am trying to import
a class I have created from another file, into my current working file. The class is as follows in this example:
ClassFile
class example_class:
def __init__(self, ticker):
self.df = yf.download(ticker, start='2020-12-31')
def do_something(self):
return something
Then when I go to import it into my working file, and run the code:
WorkingFile
import ClassFile
import yfinance as yf
instance = example_class('MSFT')
I get the following error:
name 'yf' is not defined
I understand that yfinance
is not being imported somewhere but, after researching, I have not been able to understand how to import it.
Essentially, I am trying to understand how to import a class from another file alongside the dependencies within said class.
答案1
得分: 0
你要么明确导入类,以便在不使用文件分类器的情况下使用其名称(ClassFile),要么使用函数名称的分类器。您不必导入依赖模块。即
import ClassFile
instance = ClassFile.example_class('MSFT')
或者:
from ClassFile import example_class
instance = example_class('MSFT')
我假设yf已经在ClassFile中导入或声明过。所以将你的yf导入语句放在ClassFile中。
英文:
You either specifically import the class if you want to use its name without the file classifier (ClassFile) or use the Classifier with the function name. You do NOT have to import the depenency modules. ie
import ClassFile
instance = ClassFile.example_class('MSFT')
or:
from ClassFile import example_class
instance = example_class('MSFT')
I assume yf is imported or declared somehwere in ClassFile. That is put you r yf import statement in ClassFile
答案2
得分: 0
只需翻译代码部分,不包括代码中的注释和HTML编码部分:
import yahoofinance as yf
class example_class:
def __init__(self, ticker):
self.df = yf.download(ticker, start='2020-12-31')
def do_something(self):
return something
import WorkingFile
...
self.df = WorkingFile.yf.download(ticker, start='2020-12-31')
希望这对你有所帮助。
英文:
You need to import
the symbol where you use it; in ClassFile
.
import yahoofinance as yf
class example_class:
def __init__(self, ticker):
self.df = yf.download(ticker, start='2020-12-31')
def do_something(self):
return something
When you import
something, it is imported into the current scope, i.e. the current file of code.
In theory, if you left import ... yf
in WorkingFile.py
, you could say
import WorkingFile
...
self.df = WorkingFile.yf.download(ticker, start='2020-12-31')
though of course in this case, that would create a circular import; and anyway, you don't want to do this - it is completely insane. The entire job of import
is to bring in something where you actually need it.
The other way around is how you are supposed to use it; the user of ClassFile
should not need to know or care that the implementation uses yfinance
internally. A proper class should encapsulate its internals from its users, and only expose functionality that they directly need.
In some scenarios, you need to compromise; for example, if ClassFile
is a really thin wrapper around yfinance
(let's say it inherits from it and adds a new method sell_MSFT
but otherwise works identically) then of course the user needs to be familiar with yfinance
anyway, and you can defer to its documentation for most of the functionality. But still, in that case, the import yfinance
needs to go where it is actually used; in ClassFile
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论