Embedding questions

Olaf Appelt tholap at compuserve.com
Fri Dec 10 15:15:49 EST 1999


Hi Fredrik,


thanks for your comments.

> > > Or you could go on and really abuse the language
> >
> > Shall I take that as a recommendation that I shouldn't use Python for
what I
> > need?
>
> nope.  you should think a bit more about your
> original problem.

OK. I need to know whether a given identifier was accessed (assigned a new
value or used in an expression).
I'm open for suggestions to achieve this other than by defining a set of
classes that carry that information:

class MyVariable
   def __init__ (self, default = 0)
      self.value = default
      self.readAccessed = 0
      self.writeAccessed = 0

   def assign (self, other)        # ugly
      self.value = other
      self.writeAccessed = 1

   def __add__ (self, other)
      r = MyVariable (self.value + other)
      self.readAccessed = 1
      return r

   #... all remaining arithmetic operators

Something like the above would result in what I need, I think. Except that
assignment would look bad.

Plan A was to define a set of classes like the above.
Let the user do some arithmetic with them, plus a few ifs, fors and whiles
and then examine the instances from C++ (retrieving value and state
information).

> if I understand things correctly, you want to
> set a variable to something, execute some user
> code, and then inspect that variable.  right?

Sounds right.

> okay, here's how to do that:
>
>     c = "my value"
>
>     namespace = {}
>     namespace["c"] = c
>
>     exec usercode in namespace
>
>     c = namespace["c"]
>
>     print c
>
> in other words, let the user change "c" to whatever
> he/she wants, and pick up the result afterwards.  if
> it's not the right type, convert it (if that's a reason-
> able thing to do).  otherwise, complain to the user.

OK, but I see two problems with this:
1. I still don't know whether it was accessed for read and/or write.
2. I can complain that the variable has an invalid value, but I can't tell
when exactly it changed to that value, which would be important for the
target audience (folks who know the problem domain, but not much about
programming).


Olaf






More information about the Python-list mailing list