Assigning 'nochage' to a variable.

C

cjfrovik

Has anyone else felt a desire for a 'nochange' value
resembling the 'Z'-state of a electronic tri-state output?

var1 = 17
var1 = func1() # func1() returns 'nochange' this time
print var1 # prints 17

It would be equivalent to:

var1 = 17
var2, bool1 = func1()
if bool1:
var1 = var2
print var1 # prints 17

BR/ CJF
 
D

Diez B. Roggisch

Has anyone else felt a desire for a 'nochange' value
resembling the 'Z'-state of a electronic tri-state output?

No. but if you must, you can emulate that using properties or
__getattr__/__setattr__- of course then limited to classes, but writen
c.var1 instead of var1 where c is your container - that world work, if
the semantics you need are so commeon it really saves you effort.

class Container(object):

def __init__(self, nc):
"""
@param nc: a list or set of names that should not be
overwritten when None is assigned to them.
"""
self.nochanges = nc

def __setattr__(self, name, value):
if name in self.nochanges and value is not None:
self.__dict__[name] = value

Diez
 
T

Terry Reedy

Has anyone else felt a desire for a 'nochange' value
No.

resembling the 'Z'-state of a electronic tri-state output?

Not familiar with that.
var1 = 17
var1 = func1() # func1() returns 'nochange' this time
print var1 # prints 17

It would be equivalent to:

var1 = 17
var2, bool1 = func1()
if bool1:
var1 = var2
print var1 # prints 17

Simpler is

var1 = 17
var2 = func1()
if var2 is not 'nochange': var1 = var2

or
var1 = func2(var1)
with func2 conditionally returning the input instead of 'nochange'.
The default return, of course, could be something other than var1.

Conclusion: you can easily do what you want with Python as it is.

Terry J. Reedy
 
C

cjfrovik

Thanks for the suggestions, although I'll probably continue to use:

var1 = 17
var1 = func1(var1)
print var1

and have func1 return the input value if no change is required.
It's *almost* as nice as if there was a Nochange value available..

BR /CF
 
C

cjfrovik

Oh, right.... I see you also thought of that.
(Sorry, didnt read your entire mail.)
 
B

Bengt Richter

Thanks for the suggestions, although I'll probably continue to use:

var1 = 17
var1 = func1(var1)
print var1

and have func1 return the input value if no change is required.
It's *almost* as nice as if there was a Nochange value available..

BR /CF
This seems to me a tiny snippet from a larger context, like maybe
simulating tranfer of info between circuits attached to a bus.
Posting tiny excerpts can only get you help with that, which in
the larger context might be fine or not, but no one can say ;-)

Regards,
Bengt Richter
 
T

Terry Hancock

Not familiar with that.

"Tri-state" logic gate outputs can do one of three things:

1) They can drive the voltage to 0.0 "0"
2) They can drive the voltage to VCC "1"
3) They can act like they aren't connected at all "Z"

The "Z" actually is the standard symbol for "impedence" (in
DC circuits this is just a fancy way to say "resistance"), and
is short for "high-Z state" or "high impedence state", which
is very much like what would happen if you just cut the wire.

Sending gates into the Z state is what allows computer buses
to work at all -- only the addressed register is allowed to
control the bus wires, all other connected devices are in the
Z state.

The software equivalent is throwing certain terms out of
a sum. We usually do that with control flow. Doing it
implicitly by value seems like it could be dangerous, but
I'd have to see a more complete implementation example to
be convinced one way or the other about that.

Cheers,
Terry
 
D

Dennis Lee Bieber

Sending gates into the Z state is what allows computer buses
to work at all -- only the addressed register is allowed to
control the bus wires, all other connected devices are in the
Z state.
My old digital electronics class could argue that "work at all"...
TTL chips with "open collector" outputs weren't "tri-state" in the sense
used here. These used external resistors to pull the line state to High
/except/ when one of the attached devices deliberately drove it low.

Tri-state, to me, implies a circuit that can be driven to either
high or low, but when undriven floats in the dead zone (for CMOS, low is
<30% of VCC, high is >70% of VCC, 30-70% is "unset") and doesn't load
down other connected devices -- though I will concede the key item is
that the third state is that the device does not drive the signal line
in either direction.

In the present discussion however... I think I'd agree with your
comments (snipped)... This "third state" is not a special value, but
rather the absence of any value (technically, this would also mean the
absence of "None" as a return) -- and the only reasonable way to produce
such an effect in code is to use conditional logic to detect the
condition and handle it as needed.
--
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top