将Python类扩展到一个文件中,以便可以在另一个文件中导入和使用。

发布于 2025-01-17 18:41:47 字数 1628 浏览 1 评论 0原文

我在 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()

我可以看到 nameidtag

我不想这样进去修改这个函数。

有没有办法可以导入模块并添加这个额外的函数,以便搜索文件可以访问这个新函数?

我尝试过这个,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱给你人给你 2025-01-24 18:41:47

您的帖子说: 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 the id, or modify that instance using: s.name = 'new name' etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文