Embedding questions

Quinn Dunkan quinn at ngwee.ugcs.caltech.edu
Thu Dec 9 00:25:52 EST 1999


On Thu, 9 Dec 1999 03:52:25 +0100, Olaf Appelt <tholap at compuserve.com> wrote:
>The added functionality we need could probably be done via classes. And here
>is already my first problem.
>It is important that the programmer can use pre-defined named variables with
>particular properties.
>In short I need to have types.
>
>All I need can be implemented with classes (I think). Luckily Python allows
>defintinition of operator methods so I can make this classes mostly behave
>like typical variables.
>Sadly I cannot redefine assignment.
>
>class Currency
>...
>
>c = Currency ()
>
>c = 5

Why not do the usual thing:

c = Currency(5)

>A possible solution would be to define something like
>
>class Currency
>...
>    def assign (self, other)
>        self.value = other
>...
>
>but that would look rather ugly:
>
>c.assign (a + b)
>
>instead of the usual
>
>c = a + b

If a and b have __add__ methods that return a Currency, c will be a Currency,
and c = a + b will work as expected.  Even if a is a Currency(5) and b is int
10, you could have Currency __coerce__ the int into a Currency.

>Furthermore I want to avoid having module files lying around in directories.
>The code should be compiled, go into database and later be read from db to
>be executed. All that without going files. Just strings moved between Python
>API and DB.
>
>Is that possible?

Should be.  See the pickle and marshal modules, and the compile builtin.



More information about the Python-list mailing list