late bindings ???

Bengt Richter bokr at oz.net
Mon Dec 2 19:20:18 EST 2002


On 02 Dec 2002 21:22:38 +0800, "Alfredo P. Ricafort" <alpot at mylinuxsite.com> wrote:

>Hi,
>
>I'm trying to create a flexible python program where the called
>functions are stored in a list.  But before you can assign the function
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The "called functions"? -- or strings representing names that you associate with functions?

>name in a list, it must be in the namespace. However, in my case, the
When you say "assign the function name in a list" I take it you mean
something like

    aList=[1,2,3]
    def foo(): print 'hi from foo'
    aList[1] = foo
    ^^^^^^^^^^^^^^

The name foo is bound to the function object created with the def foo ...
but what goes in the list (replacing the 2 in this case) is NOT a name.
It's a reference to the the same object as the name (foo here) is bound to, but you
can delete (del foo) the name, and the reference to the function will still be in the list.


>function names are known later. So what I did was to store it as a
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-- This could happen lots of ways. If you would
illustrate with actual attempted code snippets it would be easier to tell what you are
trying to do.

>string instead and do something like this:
>
>func=['do_file','do_exit']
>apply(func[i],[args])
>
>But this doesn't work!
Well, suppose i were equal to 1. Would you expect

    apply('do_exit',[args])

to work? What does apply take as arguments?
   ______
__/ Hint \________________________________________________________________________

 >>> help(apply)
 Help on built-in function apply:

 apply(...)
     apply(object[, args[, kwargs]]) -> value

     Call a callable object with positional arguments taken from the tuple args,
            ^^^^^^^^^^^^^^^
     and keyword arguments taken from the optional dictionary kwargs.
     Note that classes are callable, as are instances with a __call__() method.
__________________________________________________________________________________
>
>Now, I am looking for something that can 'typecast' a variable into a
I suspect you will have to revise your concept of "variable" to grok python.

Names do not designate memory locations. Names are like name tags with long
strings that are bound to a single object at the other end. When you do an "assignment"
you are putting a name tag into some "name space" with the other end of
the string attached to some object (usually represented in the heap) that resulted
from evaluating the right hand side of the "assignment" statement. Any number of name tags
may have strings leading to the same object. (Reference counting tells how many names and
anonymous references there are at any given time). Detaching a string does not
destroy an object, though when the last string is detached the object becomes inaccessible
and is collectable garbage.

>function. But it appears that there is none. It seems that in python
There is no 'typecast'. But you can rebind a name any time. You detach
the string and attach it so something else. The 'something else' can be
an existing thing or something new, possibly of a type that didn't always
exist either.

>late bindings are not allowed.
>
>Am I right?
>
It's more like they're all late -- and dynamic ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list