Python Forum
Class object instance. Link instance attribute to class. Can it be done easier.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class object instance. Link instance attribute to class. Can it be done easier.
#1
Just want class object instance to update all children. Every time object value changes.

Linking class object instance attributes to class.
Can it be done any easier than this.
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def set(self, x, y):
        self.x = x
        self.y = y

    def __repr__(self):
        return str(vars(self))

class Dimension:
    def __init__(self, w, h):
        self.w = w
        self.h = h

    def set(self, w, y):
        self.w = w
        self.h = h

    def __repr__(self):
        return str(vars(self))

def dir_trim(item):
    return [d for d in dir(item) if not d.startswith('__')]

class LinkProperty:
    def __init__(self, attr):
        self.attr = attr

    def __set__(self, instance, value):
        setattr(instance.read_value, self.attr, value)
        instance.callback()

    def __get__(self, instance, owner):
        return getattr(instance.read_value, self.attr)

def link_variable(value, callback):
    class LinkVariable:
        def __init__(self, value, callback):
            self.read_value = value
            self.callback = callback

        def __call__(self, value=None):
            if value is None:
                return self.read_value
            self.read_value = value
            self.callback()

        def clear(self):
            self.read_value = None
            self.callback()

    items = dir_trim(value)
    for item in items:
        setattr(LinkVariable, item, LinkProperty(item))

    return LinkVariable(value, callback)


def callback():
    print('Callback')

a = link_variable(Point(2,3), callback)
b = link_variable(Dimension(1,4), callback)
print(dir_trim(a))
print(dir_trim(b))
print(a.read_value, b.read_value)
a.x = 7
print(a.read_value, b.read_value)
print(a.x, a.y, b.w, b.h)
99 percent of computer problems exists between chair and keyboard.
Reply


Messages In This Thread
Class object instance. Link instance attribute to class. Can it be done easier. - by Windspar - Dec-03-2018, 12:48 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How does this code create a class? Pedroski55 6 447 Apr-21-2024, 06:15 AM
Last Post: Gribouillis
  class definition and problem with a method HerrAyas 2 271 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  Printing out incidence values for Class Object SquderDragon 3 305 Apr-01-2024, 07:52 AM
Last Post: SquderDragon
  class and runtime akbarza 4 400 Mar-16-2024, 01:32 PM
Last Post: deanhystad
  Operation result class SirDonkey 6 570 Feb-25-2024, 10:53 AM
Last Post: Gribouillis
  The function of double underscore back and front in a class function name? Pedroski55 9 685 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  super() and order of running method in class inheritance akbarza 7 780 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  Class test : good way to split methods into several files paul18fr 4 494 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  Good class design - with a Snake game as an example bear 1 1,846 Jan-24-2024, 08:36 AM
Last Post: annakenna
  question about __repr__ in a class akbarza 4 624 Jan-12-2024, 11:22 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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