set built-in func_code?

Discussion in 'Python' started by Birgit Rahm, Sep 9, 2003.

  1. Birgit Rahm

    Birgit Rahm Guest

    Hello newsgroup,

    I want to set the built-in func_code in my function, how can I does this in
    the correct way?
    My books dont tell me anything about this.

    TIA, Birgit
     
    Birgit Rahm, Sep 9, 2003
    #1
    1. Advertising

  2. Birgit Rahm wrote:
    > Hello newsgroup,
    >
    > I want to set the built-in func_code in my function, how can I does this in
    > the correct way?


    I don't think that's possible for an existing function object.
    Dynamically creating new function objects, however, certainly /is/ possible.

    > My books dont tell me anything about this.


    There's almost certainly a better way of achieving what you want. Why
    don't you tell us what you really want to do, i. e. what is the purpose
    of this?

    -- Gerhard
     
    =?ISO-8859-1?Q?Gerhard_H=E4ring?=, Sep 9, 2003
    #2
    1. Advertising

  3. Gerhard Häring wrote:
    > Birgit Rahm wrote:
    >> I want to set the built-in func_code in my function, how can I does
    >> this in the correct way?

    >
    > I don't think that's possible for an existing function object. [...]


    Well, maybe I should try before I post:

    #v+
    >>> def f(): print 5

    ....
    >>> def g(): print 6

    ....
    >>> f.func_code = g.func_code
    >>> f()

    6
    #v-

    This is an unnecessary hack, IMO. Hacks aren't bad, but unecessary ones
    are, IMO. Still, what are you trying to achieve?

    -- Gerhard
     
    =?ISO-8859-1?Q?Gerhard_H=E4ring?=, Sep 9, 2003
    #3
  4. Birgit Rahm

    Birgit Rahm Guest

    Hallo,
    I have a program (not written by me :-( ) that loads socalled helpfunctions
    and executes that dynamically.
    One error message was : ... cant get the func_code of the helpfunctionX.
    Which reason this message has, I dont now.
    My main problem is, that I dont know what func_code is : a built-in function
    or attribute or what ? And why is it used.

    >> I want to set the built-in func_code in my function, how can I does
    >> this in the correct way?

    >
    > I don't think that's possible for an existing function object. [...]


    Well, maybe I should try before I post:

    #v+
    >>> def f(): print 5

    ....
    >>> def g(): print 6

    ....
    >>> f.func_code = g.func_code
    >>> f()

    6
    #v-

    By the way, what's about __import__?
    I hope it is not so confusing as I feel ...
    Birgit
     
    Birgit Rahm, Sep 9, 2003
    #4
  5. Birgit Rahm wrote:
    > Hallo,
    > I have a program (not written by me :-( ) that loads socalled helpfunctions
    > and executes that dynamically.
    > One error message was : ... cant get the func_code of the helpfunctionX.


    That sounds strange. But the reason is most probably that helpfunctionX
    is in fact not a function.

    > Which reason this message has, I dont now.
    > My main problem is, that I dont know what func_code is : a built-in function
    > or attribute or what ? And why is it used.


    I don't know why it's used either, because I don't know the program you
    use :) I think you're relatively new to Python (only saw you posting
    here recently) :) So what we're touching here is advanced stuff, but
    I'll try to explain it as good as I can:

    *Everything* in Python is an object, including functions. Here's an
    excerpt from an interactive session:

    >>> def times2(x): return 2*x

    ....
    >>> times2

    <function times2 at 0x007E68F0>
    >>> type(times2)

    <type 'function'>
    >>>


    So, times2 is a function object. Being an object, it also has attributes:

    >>> dir(times2)

    ['__call__', '__class__', '__delattr__', '__dict__', '__doc__',
    '__get__', '__getattribute__', '__hash__', '__init__', '__module__',
    '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
    '__setattr__', '__str__', 'func_closure', 'func_code', 'func_defaults',
    'func_dict', 'func_doc', 'func_globals', 'func_name']

    Now it's relatively clear what func_name might be:

    >>> times2.func_name

    'times2'

    But there's also more advanced stuff like the func_code attribute.
    func_code is a code object (yes, *again* an object), that contains the
    Python bytecode for the function in question. If you really wanted to,
    you could replace the code object of the function with a new code
    object, that does something totally different. This is probably one of
    the deepest hacks you can do in Python :-D

    So if, for example, I wanted times2 to return "x*3" instead of "x*2", I
    could use something like:

    def changeMeaningOfTimes2():
    def __tmpfunc(x):
    return x*3

    times2.func_code = __tmpfunc.func_code

    As I already said, this is a very ugly hack, that is seldom, if ever
    justified.

    If you're just trying to make a call to times2() invoke something
    different, you could just do:

    def __tmpfunc(x): return 3*x
    times2 = __tmpfunc()

    because all this does is bind the name 'times2' to a different function
    object.

    I hope this gives you an insight in the possibilites of what you *can*
    do with Python. And that not everything you *can* do is necessarily a
    good idea ;-)

    That being said, what you described sounds to me like the real problem
    is that a name helpfunctionX is assigned to something other than a
    function and that your program tries to get the func_code attribute from
    this object, which fails, because it's not a function but something else.

    ciao,

    -- Gerhard
     
    =?ISO-8859-1?Q?Gerhard_H=E4ring?=, Sep 9, 2003
    #5
  6. Birgit Rahm wrote:
    > By the way, what's about __import__?


    __import__ lets you dynamically import modules. The most common use case
    is if you only know at runtime, which module you want to import.
    Programs that have some kind of plugin architecture use __import__, for
    example.

    -- Gerhard
     
    =?ISO-8859-1?Q?Gerhard_H=E4ring?=, Sep 9, 2003
    #6
  7. Birgit Rahm

    John Roth Guest

    "Birgit Rahm" <> wrote in message
    news:bjk4v2$6iv$07$-online.com...
    >
    >
    > Hello newsgroup,
    >
    > I want to set the built-in func_code in my function, how can I does this

    in
    > the correct way?


    > My books dont tell me anything about this.


    Check the index of the Python Reference Manual. You don't,
    in general, want to set that value even though it's settable. At least,
    you don't unless you've got an unusual amount of dynamism in your
    program.

    I'd suspect the reason your help system is looking for the
    code object is to find out the function name, which is in
    the compiled code object.

    John Roth
    >
    > TIA, Birgit
    >
    >
     
    John Roth, Sep 9, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Gresty
    Replies:
    0
    Views:
    528
    David Gresty
    Jul 6, 2003
  2. Andrew Degtiariov
    Replies:
    2
    Views:
    483
    Andrew Degtiariov
    Sep 17, 2004
  3. Filip Dreger

    func_code vs. string problem

    Filip Dreger, Apr 23, 2005, in forum: Python
    Replies:
    8
    Views:
    2,728
    Filip Dreger
    Apr 24, 2005
  4. Steven Watanabe
    Replies:
    0
    Views:
    323
    Steven Watanabe
    May 4, 2006
  5. Jack Diederich
    Replies:
    1
    Views:
    874
    Serge Orlov
    May 4, 2006
Loading...

Share This Page