assignment hook

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • paul.lafollette@gmail.com

    #1

    assignment hook

    Kind people,
    Is there any way one can, within Python, intercept the act of
    assignment. For instance, suppose that I was obsessed with
    FORTRAN II, and decided that I wanted to print a warning,
    or raise an exception any time someone assigned an int to a
    variable whose name did not start with i,j,k,l,m, or n.

    If assigment were an operator, I could, perhaps, subclass
    something and override the (non-existant)__assi gn__ method
    to find the name of the variable and the type of the assigned
    object, but it ain't so I can't.

    My guess is that it is simply impossible, but if it IS possible,
    I would love to know how to do it. Thank you for your time
    and expertise.

    Paul

    ---------------------------------------
    Your task it is, amid confusion, rush, and noise, to grasp the lasting,
    calm, and meaningful, and finding it anew, to hold and treasure it.
    -Paul Hindemith-

    Paul S. LaFollette, Jr.
    Temple University
    CIS Department
    paul.lafollette @temple.edu

    +1 215 204 6822



  • Roy Smith

    #2
    Re: assignment hook

    paul.lafollette @gmail.com wrote:
    Kind people,
    Is there any way one can, within Python, intercept the act of
    assignment.
    Sure. You just need to define a __setattr__() method for your class. See
    http://docs.python.org/ref/attribute-access.html for details.
    For instance, suppose that I was obsessed with
    FORTRAN II, and decided that I wanted to print a warning,
    or raise an exception any time someone assigned an int to a
    variable whose name did not start with i,j,k,l,m, or n.
    Well, you know the old joke, "You can write Fortran in any language" :-)

    Comment

    • John Machin

      #3
      Re: assignment hook


      Roy Smith wrote:
      paul.lafollette @gmail.com wrote:
      Kind people,
      Is there any way one can, within Python, intercept the act of
      assignment.
      >
      Sure. You just need to define a __setattr__() method for your class. See
      http://docs.python.org/ref/attribute-access.html for details.
      Is it possible that at least one of you has the concepts of
      "assignment " and "binding" a little mixed?
      >
      For instance, suppose that I was obsessed with
      FORTRAN II, and decided that I wanted to print a warning,
      or raise an exception any time someone assigned an int to a
      variable whose name did not start with i,j,k,l,m, or n.
      >
      Well, you know the old joke, "You can write Fortran in any language" :-)
      Yes, but it's a bit difficult to express old FORTRAN jokes in another
      language:

      real = float
      integer = int
      assert isinstance(God, real) and isinstance(Jesu s, integer)

      Cheers,
      John

      Comment

      • Georg Brandl

        #4
        Re: assignment hook

        paul.lafollette @gmail.com wrote:
        Kind people,
        Is there any way one can, within Python, intercept the act of
        assignment. For instance, suppose that I was obsessed with
        FORTRAN II, and decided that I wanted to print a warning,
        or raise an exception any time someone assigned an int to a
        variable whose name did not start with i,j,k,l,m, or n.
        This is not possible. Assignments in the form

        name = value

        (also called "binding a name") are not overloadable. As Roy Smith already noted,
        assignments in the form

        name.attribute = value
        name[item] = value

        are overloadable via __setattr__ and __setitem__.

        Georg

        Comment

        • Fredrik Lundh

          #5
          Re: assignment hook

          Georg Brandl wrote:
          This is not possible. Assignments in the form
          >
          name = value
          >
          (also called "binding a name") are not overloadable.
          footnote: unless you execute the code in a controlled environment:

          class mydict(dict):
          def __setitem__(sel f, key, value):
          print "SET", key, value
          self.__dict__[key] = value

          ns = mydict()

          exec "a = 10" in ns

          </F>

          Comment

          Working...