Python Forum
the best way to handle the class "value"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
the best way to handle the class "value"
#2
well, I thought a bit and decided not to use <<= operator in the generator, cause it is not used by target language.
so now it looks pretty 'syntax-safe':
class Test:

    def __init__(self, val, name):
        self._val = val
        self._name = name
        self.named = False

    def __ilshift__(self, other):
        if hasattr(other, 'val'):
            other = other.val
        self.set(other)
        return self

    def __rlshift__(self, other):
        return self.get()

    def set(self, val):
        self._val = val

    def get(self):
        if self.named:
            return self._name
        return self._val

    @property
    def val(self):
        return self._val


x = Test(1, 'x')
y = Test(2, 'y')

print('x.val =', x.val)
print('y.val =', y.val)

x <<= y
print('x.val =', x.val)
z: int = None
z <<= x
print('z =', z)
x <<= 3
y <<= x
print('y.val =', y.val)
y.val = 4
Output:
x.val = 1 y.val = 2 x.val = 2 z = 2 y.val = 3 Traceback (most recent call last): File "E:\packages\pyksp\pyksp\compiler2\simple_test.py", line 45, in <module> y.val = 4 AttributeError: can't set attribute
Reply


Messages In This Thread
RE: the best way to handle the class "value" - by Levitanus - Jul-22-2018, 08:30 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pythonic way to handle/spread alerts class in multiple modules psolar 11 4,693 Feb-12-2020, 04:11 PM
Last Post: psolar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020