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?
#1
i have a long (over 200, potential a lot more) list of names. for each name i need to create a function of that name in the local namespace. each function will basically be the same, using the same common code. but the logic of this code needs to know what its name is. is there a way to do that? is there a way to set that up? the function will make a request of a remote server and the action name (camel case) of that request will be derived from the name of the function (snake case). the function will do the conversion then send the request.

i tried making a deep copy of a function but when i set .__name__ on one of them it changes it to that on the copies, too. so it's still just a reference to one function.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
If its the same function just with a different name, you only need one function, pass the name as a argument to the function.
Reply
#3
passing the name is what i am doing now. i'm wanting to make all of these work just like real functions so that instead of:
    do_request('foobar','blah')
i can do:
    foobar('blah')
i do not have the list of names until run time. if i code a call that turns out not to be in the list, it should fail like any other attempt to call a function that does not exist.

the botocore library does something similar. it creates an object in which it adds a bunch of functions as attributes so you end up with a call like:
    object.associate_vpc_cidr_block(**options)
if i could do this with attributes on one object that would be nearly as good.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#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
#5
then my next question is where/how would someone have come to know that this can be done in python?
Tradition is peer pressure from dead people

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


Forum Jump:

User Panel Messages

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