how to write-protect names

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gregor Lingl

    how to write-protect names

    Hi,

    is it possible to "write-protect" a definite
    set of names of a module, especially of the
    __main__ module?
    If so, how?

    Regards, Gregor

  • Peter Hansen

    #2
    Re: how to write-protect names

    Gregor Lingl wrote:[color=blue]
    >
    > is it possible to "write-protect" a definite
    > set of names of a module, especially of the
    > __main__ module?
    > If so, how?[/color]

    Almost anything is possible at some level in Python, although,
    likewise, almost anything can be broken at another level.

    What exactly do you need this for? If you describe the purpose
    you have in mind for it we can provide the best approach, or
    tell you that we don't think you should bother. ;-)

    -Peter

    Comment

    • Gregor Lingl

      #3
      Re: how to write-protect names

      Peter Hansen schrieb:
      [color=blue]
      > What exactly do you need this for? If you describe the purpose
      > you have in mind for it we can provide the best approach, or
      > tell you that we don't think you should bother. ;-)
      >
      > -Peter[/color]

      I'm writing a module for teaching young students. It contains
      e. g. a function width, which assigns a value to some (hidden)
      variable:
      [color=blue][color=green][color=darkred]
      >>> width(5)[/color][/color][/color]

      Now my experience is, that from time to time some of my
      students write (erroneously)
      [color=blue][color=green][color=darkred]
      >>> width = 5[/color][/color][/color]

      which renders the function width unaccessible for future
      use. (Moreover the next
      [color=blue][color=green][color=darkred]
      >>> width(10)[/color][/color][/color]

      will result in a rather strange error-message, like
      int-object is not callable ... (my students won't
      underswtand it).

      Wouldn't it be useful if the name width were write-protected

      Regards,
      Gregor

      Comment

      • Peter Hansen

        #4
        Re: how to write-protect names

        Gregor Lingl wrote:[color=blue]
        >
        > Peter Hansen schrieb:
        >[color=green]
        > > What exactly do you need this for? If you describe the purpose
        > > you have in mind for it we can provide the best approach, or
        > > tell you that we don't think you should bother. ;-)
        > >
        > > -Peter[/color]
        >
        > I'm writing a module for teaching young students. It contains
        > e. g. a function width, which assigns a value to some (hidden)
        > variable:
        >[color=green][color=darkred]
        > >>> width(5)[/color][/color]
        >
        > Now my experience is, that from time to time some of my
        > students write (erroneously)
        >[color=green][color=darkred]
        > >>> width = 5[/color][/color]
        >
        > which renders the function width unaccessible for future
        > use.[/color]

        Ah, good. In that case the answer is fairly simple. You
        cannot "write-protect" the name in the main module, but you
        could use your own namespace for the methods such as width(),
        putting them in something that looks like a module but is
        really an object with a __setattr__() method which prevents
        "overwritin g" any of the existing names. Maybe util.width()
        or something like that.

        The fundamental issue is really that names are merely labels
        for the things themselves, and can be rebound at will. The
        students aren't really overwriting anything, and the original
        width() method still exists (if any other binding to it exists
        anywhere), they are simply making the label "width" reference
        a different object and you can't that without providing your
        own interactive environment, I suspect.

        (And as a result, you still can't stop the students from binding
        the name "util" to something else and still screwing up the above,
        but perhaps you can trust the students not to do this if you
        demonstrate it and explain why it's a bad idea.)

        -Peter

        Comment

        • python

          #5
          Re: how to write-protect names

          Can you post a trivial example of how to use __setattr__() and how to
          set the namespace in the interpreter?

          Thanks.


          On Wed, 17 Sep 2003 17:42:19 -0400
          Peter Hansen <peter@engcorp. com> wrote:
          [color=blue]
          > Gregor Lingl wrote:[color=green]
          > >
          > > Peter Hansen schrieb:
          > >[color=darkred]
          > > > What exactly do you need this for? If you describe the purpose
          > > > you have in mind for it we can provide the best approach, or
          > > > tell you that we don't think you should bother. ;-)
          > > >
          > > > -Peter[/color]
          > >
          > > I'm writing a module for teaching young students. It contains
          > > e. g. a function width, which assigns a value to some (hidden)
          > > variable:
          > >[color=darkred]
          > > >>> width(5)[/color]
          > >
          > > Now my experience is, that from time to time some of my
          > > students write (erroneously)
          > >[color=darkred]
          > > >>> width = 5[/color]
          > >
          > > which renders the function width unaccessible for future
          > > use.[/color]
          >
          > Ah, good. In that case the answer is fairly simple. You
          > cannot "write-protect" the name in the main module, but you
          > could use your own namespace for the methods such as width(),
          > putting them in something that looks like a module but is
          > really an object with a __setattr__() method which prevents
          > "overwritin g" any of the existing names. Maybe util.width()
          > or something like that.
          >
          > The fundamental issue is really that names are merely labels
          > for the things themselves, and can be rebound at will. The
          > students aren't really overwriting anything, and the original
          > width() method still exists (if any other binding to it exists
          > anywhere), they are simply making the label "width" reference
          > a different object and you can't that without providing your
          > own interactive environment, I suspect.
          >
          > (And as a result, you still can't stop the students from binding
          > the name "util" to something else and still screwing up the above,
          > but perhaps you can trust the students not to do this if you
          > demonstrate it and explain why it's a bad idea.)
          >
          > -Peter
          > --
          > http://mail.python.org/mailman/listinfo/python-list[/color]

          Comment

          • anton muhin

            #6
            Re: how to write-protect names

            python wrote:[color=blue]
            > Can you post a trivial example of how to use __setattr__() and how to
            > set the namespace in the interpreter?
            >
            > Thanks.
            >
            >[/color]

            Really basic one:

            class Namespace(objec t):
            def __setattr__(sel f, name, value):
            if name in self.__dict__:
            raise "oops"
            object.__setatt r__(self, name, value)

            util = Namespace()

            util.x = 1
            print util.x

            util.x = 2

            Comment

            • Lee Harr

              #7
              Re: how to write-protect names

              In article <3F68CE89.20601 @aon.at>, Gregor Lingl wrote:[color=blue]
              > Peter Hansen schrieb:
              >[color=green]
              >> What exactly do you need this for? If you describe the purpose
              >> you have in mind for it we can provide the best approach, or
              >> tell you that we don't think you should bother. ;-)
              >>
              >> -Peter[/color]
              >
              > I'm writing a module for teaching young students. It contains
              > e. g. a function width, which assigns a value to some (hidden)
              > variable:
              >[color=green][color=darkred]
              > >>> width(5)[/color][/color]
              >
              > Now my experience is, that from time to time some of my
              > students write (erroneously)
              >[color=green][color=darkred]
              > >>> width = 5[/color][/color]
              >
              > which renders the function width unaccessible for future
              > use. (Moreover the next
              >[color=green][color=darkred]
              > >>> width(10)[/color][/color]
              >
              > will result in a rather strange error-message, like
              > int-object is not callable ... (my students won't
              > underswtand it).
              >
              > Wouldn't it be useful if the name width were write-protected
              >[/color]


              Maybe you could do something with the code module
              (InteractiveCon sole, compile_command , etc)

              You get to set up a hook that is called each time a new
              input line comes in... so you could watch for your special
              names and just print a helpful reminder when someone tries
              to overwrite one.

              Comment

              Working...