将Python类扩展到一个文件中,以便可以在另一个文件中导入和使用。
我在 python 中有以下模块,我使用 pip 下载了这些模块。
mainmodule
|
|--utils
| |
| |--search_obj.py
|
|--search.py
search
看起来像这样,
import requests
from unidecode import unidecode
from .utils.search_obj import SearchObj
def search_text(text):
..............
..............
search_obj.py
看起来像这样,
class SearchObj(object):
def __init__(self, name, id, tag):
self.name = name
self.id = id
self.tag = tag
def some functions():
...........
...........
所以要执行它,我这样做,
mainmodule.search.search_text('testword')
它只是返回一个 SearchObj。
我想打印姓名、id 和标签。
如果我进入模块并创建一个像这样的新函数,
class SearchObj(object):
def __init__(self, name, id, tag):
self.name = name
self.id = id
self.tag = tag
def print_stuff():
return self.id, self.name, self.tag
然后尝试,
mainmodule.search.search_text('testword').print_stuff()
我可以看到 name
、id
和 tag
。
我不想这样进去修改这个函数。
有没有办法可以导入模块并添加这个额外的函数,以便搜索文件可以访问这个新函数?
我尝试过这个,
class finder (SearchObj):
def __init__(self):
super(SearchObj, self)
def print_stuff(self):
return self.tag, self.name, self.id
但是当我运行这个时,我不断收到错误。
finder.search.search_text('testword').print_stuff()
这是错误,
AttributeError: type object 'finder' has no attribute 'search'
I have the following modules in python which I downloaded using pip.
mainmodule
|
|--utils
| |
| |--search_obj.py
|
|--search.py
The search
looks like this,
import requests
from unidecode import unidecode
from .utils.search_obj import SearchObj
def search_text(text):
..............
..............
and search_obj.py
looks like this,
class SearchObj(object):
def __init__(self, name, id, tag):
self.name = name
self.id = id
self.tag = tag
def some functions():
...........
...........
So to execute it I do this,
mainmodule.search.search_text('testword')
It just returns me a SearchObj.
I want to print the name, id and tag.
If I go into the module and create a new function like this,
class SearchObj(object):
def __init__(self, name, id, tag):
self.name = name
self.id = id
self.tag = tag
def print_stuff():
return self.id, self.name, self.tag
Then try,
mainmodule.search.search_text('testword').print_stuff()
I can see the name
, id
and tag
.
I don't want to go in and modify the function this way.
Is there a way I can import the module and just add this extra function so that the search file can access this new function?
I tried this,
class finder (SearchObj):
def __init__(self):
super(SearchObj, self)
def print_stuff(self):
return self.tag, self.name, self.id
But I keep getting an error, when I run this.
finder.search.search_text('testword').print_stuff()
This is the error,
AttributeError: type object 'finder' has no attribute 'search'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的帖子说:
mainmodule.search.search_text('testword').print_stuff()
返回 id、名称和标签,以便您可以看到它,因此如下:s = mainmodule.search.search_text('testword')
将返回
SearchObj
的实例。现在您可以执行:
print(s.id)
以查看id
,或使用以下命令修改该实例:s.name = 'new name'
等等Your post says that this:
mainmodule.search.search_text('testword').print_stuff()
returns the id, name and tag so you can see it, so it follows that:s = mainmodule.search.search_text('testword')
will return an instance of
SearchObj
.Now you can do:
print(s.id)
to see theid
, or modify that instance using:s.name = 'new name'
etc.