简体   繁体   中英

Python How to call a class method without importing

I found this piece of code in this repo. https://github.com/adw0rd/instagrapi/blob/master/instagrapi/mixins/fbsearch.py

And I wonder how self.private_request() could be called because there is no import and private_request() isn't defined in this 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

It would be glad if someone could explain why and how this is possible.

This code doesn't call fbsearch_places ; only defines it. If it called it without defining private_request , that would cause an exception.

But if someone imports this code and uses this class in combination with another class where private_request is defined, then they will be able to use the fbsearch_places method.

That's what "mix in" means in FbSearchMixin : that the class that is supposed to be used in combination with another class in multiple inheritance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM