alias for data member of class instance?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sean McIlroy

    #1

    alias for data member of class instance?

    hi all

    is there a way to do this ...

    class clown:
    def __init__(self):
    self.x = 0
    self.y = ALIAS(self.x) ## FEASIBLE ?

    .... so that you get results like this ...

    krusty = clown()
    krusty.x
    >0
    krusty.y
    >0
    krusty.x = 1
    krusty.x
    >1
    krusty.y
    >1
    .... ? thanks.

    peace
    stm

  • Paul Rubin

    #2
    Re: alias for data member of class instance?

    "Sean McIlroy" <sean_mcilroy@y ahoo.comwrites:
    self.y = ALIAS(self.x) ## FEASIBLE ?
    The closest thing is probably to use @property.

    Comment

    • Bruno Desthuilliers

      #3
      Re: alias for data member of class instance?

      Sean McIlroy a écrit :
      hi all
      >
      is there a way to do this ...
      >
      class clown:
      def __init__(self):
      self.x = 0
      self.y = ALIAS(self.x) ## FEASIBLE ?
      >
      class Clown(object):
      def __init__(self):
      self.x = 0

      @apply
      def x():
      def fget(self):
      return self._x
      def fset(self, value):
      self._x = value
      return property(**loca ls())

      y = x

      Comment

      • Larry Bates

        #4
        Re: alias for data member of class instance?

        Sean McIlroy wrote:
        Sean McIlroy wrote:
        hi all
        >
        is there a way to do this ...
        >
        class clown:
        def __init__(self):
        self.x = 0
        self.y = ALIAS(self.x) ## FEASIBLE ?
        >
        ... so that you get results like this ...
        >
        krusty = clown()
        krusty.x
        >>0
        krusty.y
        >>0
        krusty.x = 1
        krusty.x
        >>1
        krusty.y
        >>1
        >
        ... ? thanks.
        >
        peace
        stm
        >
        hi all
        >
        is there a way to do this ...
        >
        class clown:
        def __init__(self):
        self.x = 0
        self.y = ALIAS(self.x) ## FEASIBLE ?
        >
        ... so that you get results like this ...
        >
        krusty = clown()
        krusty.x
        >>0
        krusty.y
        >>0
        krusty.x = 1
        krusty.x
        >>1
        krusty.y
        >>1
        >
        ... ? thanks.
        >
        peace
        stm
        >
        Not sure why you want it, but here is one solution:

        class clown:
        def __init__(self):
        self.x=0
        def __getattr__(sel f, key):
        if key == 'y': return self.x
        return self.__dict__[key]

        -Larry

        Comment

        • Bruno Desthuilliers

          #5
          Re: alias for data member of class instance?

          Sean McIlroy a écrit :
          hi all
          >
          is there a way to do this ...
          >
          class clown:
          def __init__(self):
          self.x = 0
          self.y = ALIAS(self.x) ## FEASIBLE ?
          class Alias(object):
          def __init__(self, attrname):
          self._attrname = attrname

          def __get__(self, instance, cls):
          if instance is None:
          return self
          return getattr(instanc e, self._attrname)

          def __set__(self, instance, value):
          setattr(instanc e, self._attrname, value)

          class Clown2(object):
          def __init__(self):
          self.x = 0

          y = Alias('x')



          Comment

          Working...