Overloadable Assignment PEP

Drew Moore drew at astro.pas.rochester.edu
Thu Apr 3 22:17:19 EST 2003


Jp Calderone <exarkun at intarweb.us> wrote in message news:<mailman.1049388384.29614.python-list at python.org>...

> 
>     class Conductor(object):
>         def set_voltage(self, value):
>             # Twiddle whatever needs to be twiddled
>         def get_voltage(self):
>             # Lookup whatever the voltage is
>             return it
>         voltage = property(get_voltage, set_voltage)
> 
>     c = Conductor()
>     c.voltage = 3
> 
>   This is more along the lines of what you'd really do, anyway, right? 
> After all, if "voltage" is simply a global, then your program or library
> will be forever tied to controlling a single item across which an emf can
> be put.  Not very scalable, right?
> 
>   Jp
> 
> -- 
> Lowery's Law:
>         If it jams -- force it.  If it breaks, it needed replacing anyway.

Well, not exactly what I had in mind.. how bout this?

class dacvoltage() :
    def __init__(self,dacnum,volt=0.0,dacfunc=maindac) :
        self.dacnum = dacnum
        self.volt = volt
        self.writedac=dacfunc
        self.writedac(self.dacnum,self.volt)
    def __assign__(self,other) :
        # todo: add code to check type of other..
        # if it is another dacvoltage object,
        # return other, not self...
        # this will rebind the name!
        try:
            self.writedac(self.dacnum,other) # might barf.
            self.volt=other  # didn't barf? remember what was assigned.
        except:
            # complain bitterly here if writedac barfs.
        return self # retain identity..

# instantiate some dac voltage objects for the first time..
# (or re-instantiate old ones, if above todo gets done..)

vdduc = dacvoltage(1, -4.0)
vreset = dacvoltage(2, -3.0)

vbias = dacvoltage(0,dacfunc=auxdac) # bias dac uses special converter..

vbias = 2.5

# etc etc...
# do some stuff at these voltages..


# modify the voltages, in a very simple, readable syntax.
vdduc = -3.8 # try a reduced supply
vreset = -3.2 # and increased reset drive...
vbias = 2.3 # and decreas the bias a little..

# do some more stuff with new voltages, etc etc...


Drew




More information about the Python-list mailing list