Python Forum
can functions know/learn who they are?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
can functions know/learn who they are?
#4
looking at botocore source, it looks like it does a def inside a method (defined with its own def) and renames it by assigning to .__name__:

    def _create_api_method(self, py_operation_name, operation_name,
                           service_model):
        def _api_call(self, *args, **kwargs):
            # We're accepting *args so that we can give a more helpful
            # error message than TypeError: _api_call takes exactly
            # 1 argument.
            if args:
                raise TypeError(
                    "%s() only accepts keyword arguments." % py_operation_name)
            # The "self" in this scope is referring to the BaseClient.
            return self._make_api_call(operation_name, kwargs)

        _api_call.__name__ = str(py_operation_name)

        # Add the docstring to the client method
        operation_model = service_model.operation_model(operation_name)
        docstring = ClientMethodDocstring(
            operation_model=operation_model,
            method_name=operation_name,
            event_emitter=self._event_emitter,
            method_description=operation_model.documentation,
            example_prefix='response = client.%s' % py_operation_name,
            include_signature=False
        )
        _api_call.__doc__ = docstring
        return _api_call
it looks like the created function (defined as _api_call(), initially) ends up with a value in variable operation_name somehow. can creating a function inside a function inherit that from its creator? i don't see any other way that variable gets set in _spi_call() (the inner def).

my goal is to do like this but put them in the appropriate local space, instead of attributes.

trying some code of my own i discover that if the function being created actually uses variable names that are in the outer function's local namespace, then they get included in the inner (created) function's local namespace. but it has to actually use them for this to happen. so it looks like a compiler thing.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Messages In This Thread
RE: can functions know/learn who they are? - by Skaperen - May-04-2019, 01:22 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020