![](/img/trans.png)
[英]How to call a method from a different file without importing it in 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.