簡體   English   中英

Python如何在不導入的情況下調用類方法

[英]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()沒有在這個類中定義。

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情況下調用它,則會導致異常。

但是,如果有人進口此代碼,並使用這個類與其他地方級組合private_request定義,那么他們將能夠使用fbsearch_places方法。

這就是FbSearchMixin “混入”的含義:應該在多重繼承中與另一個類結合使用的類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM