Home > Mobile >  Python How to call a class method without importing
Python How to call a class method without importing

Time:10-16

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.

CodePudding user response:

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.

  • Related