translating "setattr" to C++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eric

    translating "setattr" to C++

    Slightly off topic, i know, but here goes:

    I'm trying to xlate a module of mine to C++. Only problem is, it makes
    heavy use of "setattr". Anyone know a straightforward way to do
    "setattr" in C++ ?

    thanks,
    Eric
  • Peter Hansen

    #2
    Re: translating "setattr&q uot; to C++

    Eric wrote:
    [color=blue]
    > Slightly off topic, i know, but here goes:
    >
    > I'm trying to xlate a module of mine to C++. Only problem is, it makes
    > heavy use of "setattr". Anyone know a straightforward way to do
    > "setattr" in C++ ?[/color]

    Thinking about what setattr() does, and about how C++ works, I suspect
    there is no straightforward way. There _might_ be some existing library
    for introspection that can do something resembling what you want.

    Think about it: setattr() uses the names of arbitrary (possibly
    non-existing, but of course you can't solve that in C++ at all)
    attributes that are contained in strings. That means the resolution is
    done at run-time. In C++, on the other hand, all the names pretty much
    vanish at compile-time, so unless you prepare mappings of some kind
    ahead of time, or have a package which can read dynamically from header
    files or something, you're plain screwed. AFAICS.

    -Peter

    Comment

    • Dan Bishop

      #3
      Re: translating "setattr&q uot; to C++

      Eric <eric@lentil.co m> wrote in message news:<87oeqtkru w.fsf@subopt.au stin.rr.com>...[color=blue]
      > Slightly off topic, i know, but here goes:
      >
      > I'm trying to xlate a module of mine to C++. Only problem is, it makes
      > heavy use of "setattr". Anyone know a straightforward way to do
      > "setattr" in C++ ?[/color]

      No, but there's a workaround.

      Instead of referring to the fields of an object as, for example, x.foo
      and x.bar, store your object's data in a hash table with "foo" and
      "bar" as keys.

      Comment

      • Thomas Heller

        #4
        Re: translating &quot;setattr&q uot; to C++

        danb_83@yahoo.c om (Dan Bishop) writes:
        [color=blue]
        > Eric <eric@lentil.co m> wrote in message news:<87oeqtkru w.fsf@subopt.au stin.rr.com>...[color=green]
        >> Slightly off topic, i know, but here goes:
        >>
        >> I'm trying to xlate a module of mine to C++. Only problem is, it makes
        >> heavy use of "setattr". Anyone know a straightforward way to do
        >> "setattr" in C++ ?[/color]
        >
        > No, but there's a workaround.
        >
        > Instead of referring to the fields of an object as, for example, x.foo
        > and x.bar, store your object's data in a hash table with "foo" and
        > "bar" as keys.[/color]

        You could even store the hash table as a __dict__ instance variable in
        your C++ object. And use a PyDict_Object instead of the has table.

        ;-)

        Thomas


        Comment

        Working...