Python 如何在不导入的情况下调用 class 方法

Python How to call a class method without importing

我在这个 repo 中找到了这段代码。 https://github.com/adw0rd/instagrapi/blob/master/instagrapi/mixins/fbsearch.py

而且我想知道如何调用 self.private_request(),因为没有导入并且 private_request() 未在此 class 中定义。

from instagrapi.extractors import extract_location


class FbSearchMixin:

    def fbsearch_places(self, query: str, lat: float = 40.74, lng: float = -73.94):
        params = {
            'search_surface': 'places_search_page',
            'timezone_offset': self.timezone_offset,
            'lat': lat,
            'lng': lng,
            'count': 30,
            'query': query,
        }
        result = self.private_request("fbsearch/places/", params=params)
        locations = []
        for item in result['items']:
            locations.append(extract_location(item['location']))
        return locations

如果有人能解释为什么以及如何做到这一点,我们将很高兴。

此代码不调用 fbsearch_places;只定义它。如果它在没有定义 private_request 的情况下调用它,那将导致异常。

但是如果有人导入此代码并将此 class 与另一个 class 结合使用,其中 private_request 定义的,那么他们将能够使用 fbsearch_places 方法。

这就是 FbSearchMixin 中“混入”的意思:class 应该在多重继承中与另一个 class 结合使用。