Private data

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

    #1

    Private data



    This is something that I just threw together this morning, after a
    eureka moment. It's a way of creating private class attributes and
    static function variables (I'm not 100% sure if that's the correct
    terminology, but you get what I mean). I haven't tried to create
    private instance attributes, mainly because it would just be too
    difficult, and it would be awful syntax. I'm not considering actually
    using this, but I do have a couple questions about it.

    1. Has anyone else ever come up with something like this? I can't
    imagine I'm the only person who's ever thought of this.
    2. Is it possible to hack into something like this? ie, would it be
    possible to see and change these variables from client code (assuming
    the data manager has been properly removed from sight, as shown on the
    last line of class block TestPrivateClas sAttributes)?

    To avoid utter confusion, you may want to look at
    TestPrivateClas sAttributes before looking at the actual implementation
    of PrivateDataEngi ne. If you're still confused, feel free to ask
    what's going on here (and I'm open to suggestions on how to make it
    clearer).

  • Gabriel Genellina

    #2
    Re: Private data

    En Sat, 17 Mar 2007 13:31:01 -0300, Dustan <DustanGroups@g mail.com>
    escribió:

    >
    This is something that I just threw together this morning, after a
    eureka moment. It's a way of creating private class attributes and
    static function variables (I'm not 100% sure if that's the correct
    terminology, but you get what I mean). I haven't tried to create
    private instance attributes, mainly because it would just be too
    difficult, and it would be awful syntax. I'm not considering actually
    using this, but I do have a couple questions about it.
    I feel so dumb, but I can't see how to use it, or what's for. Perhaps an
    example?

    --
    Gabriel Genellina

    Comment

    • Steven D'Aprano

      #3
      Re: Private data

      On Sat, 17 Mar 2007 09:31:01 -0700, Dustan wrote:

      >
      This is something that I just threw together this morning, after a
      eureka moment. It's a way of creating private class attributes and
      static function variables (I'm not 100% sure if that's the correct
      terminology, but you get what I mean). I haven't tried to create
      private instance attributes, mainly because it would just be too
      difficult, and it would be awful syntax. I'm not considering actually
      using this, but I do have a couple questions about it.
      >
      1. Has anyone else ever come up with something like this? I can't
      imagine I'm the only person who's ever thought of this.
      I've never seen anything like this before, but then I haven't gone looking
      for anything like this.


      2. Is it possible to hack into something like this? ie, would it be
      possible to see and change these variables from client code (assuming
      the data manager has been properly removed from sight, as shown on the
      last line of class block TestPrivateClas sAttributes)?
      Yes.

      First, an example of the code in action.
      >>import PrivateAttribut es
      >>obj = PrivateAttribut es.TestPrivateC lassAttributes( )
      >>obj.getNumIns tances()
      1
      >>another = PrivateAttribut es.TestPrivateC lassAttributes( )
      >>obj.getNumIns tances()
      2
      >>athird = PrivateAttribut es.TestPrivateC lassAttributes( )
      >>athird.getNum Instances()
      3

      The getNumInstances method reports the number of instances of the
      PrivateAttribut es class. There's no obvious class attribute where this
      count is being kept:
      >>obj.__class__ .__dict__.keys( )
      ['__module__', 'getNumInstance s', '__dict__', '__weakref__', '__doc__',
      '__init__']


      Here's how to hack it, and make it report wrong numbers.
      >>c = obj.getNumInsta nces.func_closu re
      >>c[1].cell_contents. numInstances = -300
      >>>
      >>athird.getNum Instances()
      -300
      >>afourth = PrivateAttribut es.TestPrivateC lassAttributes( )
      >>athird.getNum Instances()
      -299

      So yes, it is absolutely hackable.

      Now, I'm hardly a Python guru, but in about fifteen minutes I followed the
      trail through the object chain, and found how to hack this. An real guru
      would probably do it in three minutes.

      I was helped a bit by having the source code. But even without the source
      code, I reckon I could have done it in an hour or so, if I was motivated
      enough. All the tools you need are a Python interactive session, the dir()
      function and the dis module.



      --
      Steven

      Comment

      • Dustan

        #4
        Re: Private data

        On Mar 18, 5:26 am, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
        wrote:
        En Sat, 17 Mar 2007 13:31:01 -0300, Dustan <DustanGro...@g mail.com>
        escribió:
        >>
        This is something that I just threw together this morning, after a
        eureka moment. It's a way of creating private class attributes and
        static function variables (I'm not 100% sure if that's the correct
        terminology, but you get what I mean). I haven't tried to create
        private instance attributes, mainly because it would just be too
        difficult, and it would be awful syntax. I'm not considering actually
        using this, but I do have a couple questions about it.
        >
        I feel so dumb, but I can't see how to use it, or what's for. Perhaps an
        example?
        >
        --
        Gabriel Genellina
        There are two examples - one demonstrating static function variables
        and one demonstrating private class attributes. Both perform some kind
        of counting. The function returns how many times it's been called.
        Here it is:

        @PrivateDataEng ine(numCalls = 0)
        def testPrivateStat icFunctionVaria bles(internalDa ta):
        """returns the number of times this function has been called."""
        internalData.nu mCalls += 1
        return internalData.nu mCalls

        There's a comment explaining how the decorator works on the page I
        linked you to.

        A common pythonic workaround for this is something like this:

        def testMutableDefa ultArgs(_cache = {'internalData' :0}):
        """returns the number of times this function has been called."""
        _cache['internalData'] += 1
        return _cache['internalData']

        The default argument only gets evaluated once, and since it's mutable
        and being modified in the function, it magically becomes
        {'internalData' :1} after 1 call and {'internalData' :1348372} after
        1348372 calls.

        The other example, explaining private class attributes, has a method
        getNumInstances that returns how many instances have been created.
        Here that is:

        class TestPrivateClas sAttributes(obj ect):
        # pca here obviously stands for Private Class Attributes.
        pcaManager = PrivateDataEngi ne(numInstances = 0)

        # Notice that the internal data is an implicit parameter that
        comes first,
        # even before the self parameter.
        @pcaManager
        def __init__(intern alData, self):
        internalData.nu mInstances += 1

        @staticmethod
        @pcaManager
        def getNumInstances (internalData):
        return internalData.nu mInstances

        # Don't forget to delete the pcvManager, or it will have all been
        in vain.
        del pcaManager

        I'm thinking this is going to look rather ugly when the text gets
        wrapped; that's why I linked to an external page in the first place.

        Notice it requires more advanced usage of the PrivateDataEngi ne if you
        want multiple functions to have access to the same data, as you
        normally would in a class.

        Note also that the double-decorated method getNumInstances requires
        that staticmethod be the first decorator. This is equivalent to the
        code "getNumInstance s = staticmethod(pc aManager(getNum Instances))". I
        got an error when I tried it with pcaManager preceding staticmethod,
        so my educated guess is that staticmethods are somehow specially
        recognized by the class, and therefore they have to actually BE
        staticmethods, not dressed over by a pcaManager.

        Now that I think of it, for clarity, pcaManager may not have been a
        good name for the function returned by PrivateDataEngi ne; pcaDecorator
        would have been better, seeing as it IS a decorator. Perhaps I'll
        change that soon.

        The pythonic way would be:

        class TestPythonsPriv ateClassAttribu tes(object):
        _numInstances = 0

        def __init__(self):
        # class attributes can be accessed from self.
        self.numInstanc es += 1

        @staticmethod
        def getNumInstances (internalData):
        return self.numInstanc es

        It should immediately stick out as being much more concise
        syntactically, and therefore much less bug-prone.

        Obviously, these examples are a lot like "Hello, World!" in their
        usefulness; one would very rarely want to track how many times a
        function has been called.

        Hope that helps.

        Comment

        • Dustan

          #5
          Re: Private data

          On Mar 18, 7:06 am, Steven D'Aprano
          <s...@REMOVE.TH IS.cybersource. com.auwrote:
          On Sat, 17 Mar 2007 09:31:01 -0700, Dustan wrote:>
          This is something that I just threw together this morning, after a
          eureka moment. It's a way of creating private class attributes and
          static function variables (I'm not 100% sure if that's the correct
          terminology, but you get what I mean). I haven't tried to create
          private instance attributes, mainly because it would just be too
          difficult, and it would be awful syntax. I'm not considering actually
          using this, but I do have a couple questions about it.
          >
          1. Has anyone else ever come up with something like this? I can't
          imagine I'm the only person who's ever thought of this.
          >
          I've never seen anything like this before, but then I haven't gone looking
          for anything like this.
          >
          2. Is it possible to hack into something like this? ie, would it be
          possible to see and change these variables from client code (assuming
          the data manager has been properly removed from sight, as shown on the
          last line of class block TestPrivateClas sAttributes)?
          >
          Yes.
          >
          First, an example of the code in action.
          >
          >import PrivateAttribut es
          >obj = PrivateAttribut es.TestPrivateC lassAttributes( )
          >obj.getNumInst ances()
          1
          >another = PrivateAttribut es.TestPrivateC lassAttributes( )
          >obj.getNumInst ances()
          2
          >athird = PrivateAttribut es.TestPrivateC lassAttributes( )
          >athird.getNumI nstances()
          >
          3
          >
          The getNumInstances method reports the number of instances of the
          PrivateAttribut es class. There's no obvious class attribute where this
          count is being kept:
          >
          >obj.__class__. __dict__.keys()
          >
          ['__module__', 'getNumInstance s', '__dict__', '__weakref__', '__doc__',
          '__init__']
          >
          Here's how to hack it, and make it report wrong numbers.
          >
          >c = obj.getNumInsta nces.func_closu re
          >c[1].cell_contents. numInstances = -300
          >
          >athird.getNumI nstances()
          -300
          >afourth = PrivateAttribut es.TestPrivateC lassAttributes( )
          >athird.getNumI nstances()
          >
          -299
          >
          So yes, it is absolutely hackable.
          I did have a feeling that it was hackable, but had no idea how one
          could possibly go about hacking it (I was starting to wonder of there
          was a way to apply locals() and globals() on functions). But now I
          (ehem) sorta know how it's done.
          Now, I'm hardly a Python guru, but in about fifteen minutes I followed the
          trail through the object chain, and found how to hack this. An real guru
          would probably do it in three minutes.
          >
          I was helped a bit by having the source code. But even without the source
          code, I reckon I could have done it in an hour or so, if I was motivated
          enough. All the tools you need are a Python interactive session, the dir()
          function and the dis module.
          I have used all of those before, but I haven't been able to fully
          understand the output of the dis module; maybe that's my problem.
          --
          Steven

          Comment

          • Dustan

            #6
            Re: Private data

            On Mar 18, 7:17 am, "Dustan" <DustanGro...@g mail.comwrote:
            On Mar 18, 5:26 am, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
            wrote:
            >
            >
            >
            En Sat, 17 Mar 2007 13:31:01 -0300, Dustan <DustanGro...@g mail.com>
            escribió:
            >>
            This is something that I just threw together this morning, after a
            eureka moment. It's a way of creating private class attributes and
            static function variables (I'm not 100% sure if that's the correct
            terminology, but you get what I mean). I haven't tried to create
            private instance attributes, mainly because it would just be too
            difficult, and it would be awful syntax. I'm not considering actually
            using this, but I do have a couple questions about it.
            >
            I feel so dumb, but I can't see how to use it, or what's for. Perhaps an
            example?
            >
            --
            Gabriel Genellina
            >
            There are two examples - one demonstrating static function variables
            and one demonstrating private class attributes. Both perform some kind
            of counting. The function returns how many times it's been called.
            Here it is:
            >
            @PrivateDataEng ine(numCalls = 0)
            def testPrivateStat icFunctionVaria bles(internalDa ta):
            """returns the number of times this function has been called."""
            internalData.nu mCalls += 1
            return internalData.nu mCalls
            >
            There's a comment explaining how the decorator works on the page I
            linked you to.
            >
            A common pythonic workaround for this is something like this:
            >
            def testMutableDefa ultArgs(_cache = {'internalData' :0}):
            """returns the number of times this function has been called."""
            _cache['internalData'] += 1
            return _cache['internalData']
            >
            The default argument only gets evaluated once, and since it's mutable
            and being modified in the function, it magically becomes
            {'internalData' :1} after 1 call and {'internalData' :1348372} after
            1348372 calls.
            >
            The other example, explaining private class attributes, has a method
            getNumInstances that returns how many instances have been created.
            Here that is:
            >
            class TestPrivateClas sAttributes(obj ect):
            # pca here obviously stands for Private Class Attributes.
            pcaManager = PrivateDataEngi ne(numInstances = 0)
            >
            # Notice that the internal data is an implicit parameter that
            comes first,
            # even before the self parameter.
            @pcaManager
            def __init__(intern alData, self):
            internalData.nu mInstances += 1
            >
            @staticmethod
            @pcaManager
            def getNumInstances (internalData):
            return internalData.nu mInstances
            >
            # Don't forget to delete the pcvManager, or it will have all been
            in vain.
            del pcaManager
            >
            I'm thinking this is going to look rather ugly when the text gets
            wrapped; that's why I linked to an external page in the first place.
            >
            Notice it requires more advanced usage of the PrivateDataEngi ne if you
            want multiple functions to have access to the same data, as you
            normally would in a class.
            >
            Note also that the double-decorated method getNumInstances requires
            that staticmethod be the first decorator. This is equivalent to the
            code "getNumInstance s = staticmethod(pc aManager(getNum Instances))". I
            got an error when I tried it with pcaManager preceding staticmethod,
            so my educated guess is that staticmethods are somehow specially
            recognized by the class, and therefore they have to actually BE
            staticmethods, not dressed over by a pcaManager.
            >
            Now that I think of it, for clarity, pcaManager may not have been a
            good name for the function returned by PrivateDataEngi ne; pcaDecorator
            would have been better, seeing as it IS a decorator. Perhaps I'll
            change that soon.
            >
            The pythonic way would be:
            >
            class TestPythonsPriv ateClassAttribu tes(object):
            _numInstances = 0
            >
            def __init__(self):
            # class attributes can be accessed from self.
            self.numInstanc es += 1
            >
            @staticmethod
            def getNumInstances (internalData):
            return self.numInstanc es
            >
            It should immediately stick out as being much more concise
            syntactically, and therefore much less bug-prone.
            >
            Obviously, these examples are a lot like "Hello, World!" in their
            usefulness; one would very rarely want to track how many times a
            function has been called.
            >
            Hope that helps.
            I forgot to note that the 'pythonic' ways are untested (and remain so,
            as far as I know).

            Comment

            • Dustan

              #7
              Re: Private data

              On Mar 18, 7:25 am, "Dustan" <DustanGro...@g mail.comwrote:
              On Mar 18, 7:06 am, Steven D'Aprano
              First, an example of the code in action.
              >
              >>import PrivateAttribut es
              >>obj = PrivateAttribut es.TestPrivateC lassAttributes( )
              >>obj.getNumIns tances()
              1
              >>another = PrivateAttribut es.TestPrivateC lassAttributes( )
              >>obj.getNumIns tances()
              2
              >>athird = PrivateAttribut es.TestPrivateC lassAttributes( )
              >>athird.getNum Instances()
              >
              3
              >
              The getNumInstances method reports the number of instances of the
              PrivateAttribut es class. There's no obvious class attribute where this
              count is being kept:
              >
              >>obj.__class__ .__dict__.keys( )
              >
              ['__module__', 'getNumInstance s', '__dict__', '__weakref__', '__doc__',
              '__init__']
              >
              Here's how to hack it, and make it report wrong numbers.
              >
              >>c = obj.getNumInsta nces.func_closu re
              >>c[1].cell_contents. numInstances = -300
              >
              >>athird.getNum Instances()
              -300
              >>afourth = PrivateAttribut es.TestPrivateC lassAttributes( )
              >>athird.getNum Instances()
              >
              -299
              >
              So yes, it is absolutely hackable.
              >
              I did have a feeling that it was hackable, but had no idea how one
              could possibly go about hacking it (I was starting to wonder of there
              was a way to apply locals() and globals() on functions). But now I
              (ehem) sorta know how it's done.
              >
              Now, I'm hardly a Python guru, but in about fifteen minutes I followed the
              trail through the object chain, and found how to hack this. An real guru
              would probably do it in three minutes.
              >
              I was helped a bit by having the source code. But even without the source
              code, I reckon I could have done it in an hour or so, if I was motivated
              enough. All the tools you need are a Python interactive session, the dir()
              function and the dis module.
              >
              I have used all of those before, but I haven't been able to fully
              understand the output of the dis module; maybe that's my problem.
              Alright, perhaps you can help me out with this learning curve here. I
              have seen, but not worked with, some basic assembly code, so I think I
              have a vague idea of what it all means, although I have a feeling it's
              not all valid assembly (on any widely used machine).

              First I dis.dis'd testPrivateStat icFunctionVaria bles:
              >>dis.dis(testP rivateStaticFun ctionVariables)
              21 0 LOAD_DEREF 0 (func)
              3 LOAD_DEREF 1 (internalData)
              6 LOAD_FAST 0 (args)
              9 CALL_FUNCTION_V AR 1
              12 RETURN_VALUE

              At first I was a little confused by this, because there's no increment
              in sight, but then I realized it was dis.dis'ing the wrapper closure
              in the internalDataDec orator closure in the PrivateDataEngi ne function
              (and then I hit myself on the head and cried out "doh!"). So that
              'code' is coming from this (taken out of closure):

              def wrapper(*args):
              return func(internalDa ta, *args)

              So, based on what you showed me, I found my way after some failed
              tries to this:
              >>dis.dis(testP rivateStaticFun ctionVariables. func_closure[0].cell_contents)
              28 0 LOAD_FAST 0 (internalData)
              3 DUP_TOP
              4 LOAD_ATTR 0 (numCalls)
              7 LOAD_CONST 1 (1)
              10 INPLACE_ADD
              11 ROT_TWO
              12 STORE_ATTR 0 (numCalls)

              29 15 LOAD_FAST 0 (internalData)
              18 LOAD_ATTR 0 (numCalls)
              21 RETURN_VALUE

              That's coming from this:

              @PrivateDataEng ine(numCalls = 0)
              def testPrivateStat icFunctionVaria bles(internalDa ta):
              """returns the number of times this function has been called."""
              internalData.nu mCalls += 1
              return internalData.nu mCalls


              Here's a few questions on this output, for which I would highly
              appreciate some answers:

              What's the difference between 'LOAD_DEREF', 'LOAD_FAST', and
              'LOAD_CONST', and, as seen at http://docs.python.org/lib/module-dis.html,
              'LOAD_GLOBAL'? I can imagine that 'LOAD_GLOBAL' loads a global, but
              seeing as python is such a dynamic language, how exactly is it
              supposed to distinguish between them?

              I don't understand the following at all: 'DUP_TOP', 'ROT_TWO'. Any
              pointers?

              What does 'INPLACE_ADD' mean, if not in place addition, and if it is
              in place addition, why does it need to 'STORE_ATTR' afterward?

              Thanks for any help!
              --
              Steven

              Comment

              • Dustan

                #8
                Re: Private data

                On Mar 18, 8:21 am, "Dustan" <DustanGro...@g mail.comwrote:
                On Mar 18, 7:25 am, "Dustan" <DustanGro...@g mail.comwrote:
                >
                >
                >
                On Mar 18, 7:06 am, Steven D'Aprano
                First, an example of the code in action.
                >
                >import PrivateAttribut es
                >obj = PrivateAttribut es.TestPrivateC lassAttributes( )
                >obj.getNumInst ances()
                1
                >another = PrivateAttribut es.TestPrivateC lassAttributes( )
                >obj.getNumInst ances()
                2
                >athird = PrivateAttribut es.TestPrivateC lassAttributes( )
                >athird.getNumI nstances()
                >
                3
                >
                The getNumInstances method reports the number of instances of the
                PrivateAttribut es class. There's no obvious class attribute where this
                count is being kept:
                >
                >obj.__class__. __dict__.keys()
                >
                ['__module__', 'getNumInstance s', '__dict__', '__weakref__', '__doc__',
                '__init__']
                >
                Here's how to hack it, and make it report wrong numbers.
                >
                >c = obj.getNumInsta nces.func_closu re
                >c[1].cell_contents. numInstances = -300
                >
                >athird.getNumI nstances()
                -300
                >afourth = PrivateAttribut es.TestPrivateC lassAttributes( )
                >athird.getNumI nstances()
                >
                -299
                >
                So yes, it is absolutely hackable.
                >
                I did have a feeling that it was hackable, but had no idea how one
                could possibly go about hacking it (I was starting to wonder of there
                was a way to apply locals() and globals() on functions). But now I
                (ehem) sorta know how it's done.
                >
                Now, I'm hardly a Python guru, but in about fifteen minutes I followed the
                trail through the object chain, and found how to hack this. An real guru
                would probably do it in three minutes.
                >
                I was helped a bit by having the source code. But even without the source
                code, I reckon I could have done it in an hour or so, if I was motivated
                enough. All the tools you need are a Python interactive session, the dir()
                function and the dis module.
                >
                I have used all of those before, but I haven't been able to fully
                understand the output of the dis module; maybe that's my problem.
                >
                Alright, perhaps you can help me out with this learning curve here. I
                have seen, but not worked with, some basic assembly code, so I think I
                have a vague idea of what it all means, although I have a feeling it's
                not all valid assembly (on any widely used machine).
                >
                First I dis.dis'd testPrivateStat icFunctionVaria bles:
                >
                >dis.dis(testPr ivateStaticFunc tionVariables)
                >
                21 0 LOAD_DEREF 0 (func)
                3 LOAD_DEREF 1 (internalData)
                6 LOAD_FAST 0 (args)
                9 CALL_FUNCTION_V AR 1
                12 RETURN_VALUE
                >
                At first I was a little confused by this, because there's no increment
                in sight, but then I realized it was dis.dis'ing the wrapper closure
                in the internalDataDec orator closure in the PrivateDataEngi ne function
                (and then I hit myself on the head and cried out "doh!"). So that
                'code' is coming from this (taken out of closure):
                >
                def wrapper(*args):
                return func(internalDa ta, *args)
                >
                So, based on what you showed me, I found my way after some failed
                tries to this:
                >
                >dis.dis(testPr ivateStaticFunc tionVariables.f unc_closure[0].cell_contents)
                >
                28 0 LOAD_FAST 0 (internalData)
                3 DUP_TOP
                4 LOAD_ATTR 0 (numCalls)
                7 LOAD_CONST 1 (1)
                10 INPLACE_ADD
                11 ROT_TWO
                12 STORE_ATTR 0 (numCalls)
                >
                29 15 LOAD_FAST 0 (internalData)
                18 LOAD_ATTR 0 (numCalls)
                21 RETURN_VALUE
                >
                That's coming from this:
                >
                @PrivateDataEng ine(numCalls = 0)
                def testPrivateStat icFunctionVaria bles(internalDa ta):
                """returns the number of times this function has been called."""
                internalData.nu mCalls += 1
                return internalData.nu mCalls
                >
                Here's a few questions on this output, for which I would highly
                appreciate some answers:
                >
                What's the difference between 'LOAD_DEREF', 'LOAD_FAST', and
                'LOAD_CONST', and, as seen athttp://docs.python.org/lib/module-dis.html,
                'LOAD_GLOBAL'? I can imagine that 'LOAD_GLOBAL' loads a global, but
                seeing as python is such a dynamic language, how exactly is it
                supposed to distinguish between them?
                >
                I don't understand the following at all: 'DUP_TOP', 'ROT_TWO'. Any
                pointers?
                >
                What does 'INPLACE_ADD' mean, if not in place addition, and if it is
                in place addition, why does it need to 'STORE_ATTR' afterward?
                >
                Thanks for any help!
                If this had been on any other usenet group, someone would have RTFM'd
                me to hell.

                I might still have some more questions later, but for now, I'll look
                at this.
                --
                Steven

                Comment

                • Gabriel Genellina

                  #9
                  Re: Private data

                  En Sun, 18 Mar 2007 10:21:27 -0300, Dustan <DustanGroups@g mail.com>
                  escribió:
                  >>>dis.dis(test PrivateStaticFu nctionVariables )
                  21 0 LOAD_DEREF 0 (func)
                  3 LOAD_DEREF 1 (internalData)
                  6 LOAD_FAST 0 (args)
                  9 CALL_FUNCTION_V AR 1
                  12 RETURN_VALUE
                  >
                  What's the difference between 'LOAD_DEREF', 'LOAD_FAST', and
                  'LOAD_CONST', and, as seen at http://docs.python.org/lib/module-dis.html,
                  'LOAD_GLOBAL'? I can imagine that 'LOAD_GLOBAL' loads a global, but
                  seeing as python is such a dynamic language, how exactly is it
                  supposed to distinguish between them?
                  The Python interpreter executes a stack-based machine. Look at
                  Python/compile.c for the code generation and Python/ceval.c for the code
                  execution.
                  LOAD_CONST pushes a constant onto the stack (all constants are previously
                  built and stored as attribute co_consts of the code object). LOAD_FAST and
                  LOAD_GLOBAL pushes a local or global variable reference. (The compiler
                  knows whether a name is local or not, just by lexical analysis).
                  LOAD_DEREF appears to be used for accessing variables inside closures, I'm
                  not sure.
                  I don't understand the following at all: 'DUP_TOP', 'ROT_TWO'. Any
                  pointers?
                  They manipulate the stack. DUP_TOP duplicates the top entry; ROT_TWO swaps
                  the two top entries.
                  What does 'INPLACE_ADD' mean, if not in place addition, and if it is
                  in place addition, why does it need to 'STORE_ATTR' afterward?
                  That corresponds to A += B. The compiler can't know if A has or not an
                  __iadd__ method; even if A implements __iadd__, that method could return a
                  different object, not always A (it *may* try to do the operation inplace,
                  but that might not always be possible). When the inplace method is not
                  implemented, __add__ is used (so effectively A += B is computed as A = A+B)

                  --
                  Gabriel Genellina

                  Comment

                  • Bruno Desthuilliers

                    #10
                    Re: Private data

                    Dustan a écrit :

                    >
                    This is something that I just threw together this morning, after a
                    eureka moment. It's a way of creating private class attributes
                    What for ? We already have one: prefix 'private' names with a single
                    underscore.
                    and
                    static function variables (I'm not 100% sure if that's the correct
                    terminology, but you get what I mean).
                    def somefunc(param, _statics={}):
                    # code here

                    I haven't tried to create
                    private instance attributes, mainly because it would just be too
                    difficult,
                    Same recipe as above : single leading underscore.
                    and it would be awful syntax. I'm not considering actually
                    using this, but I do have a couple questions about it.
                    >
                    1. Has anyone else ever come up with something like this? I can't
                    imagine I'm the only person who's ever thought of this.
                    With something like trying to forcefit access restriction in Python ?
                    Nope, you're not the first one here. I've not seen anyone using such a
                    thing in any of the projects I've worked on/with yet.

                    Comment

                    • Dustan

                      #11
                      Re: Private data

                      On Mar 19, 4:09 am, Bruno Desthuilliers <bruno.
                      42.desthuilli.. .@wtf.websitebu ro.oops.comwrot e:
                      Dustan a écrit :
                      >>
                      This is something that I just threw together this morning, after a
                      eureka moment. It's a way of creating private class attributes
                      >
                      What for ? We already have one: prefix 'private' names with a single
                      underscore.
                      >
                      and
                      static function variables (I'm not 100% sure if that's the correct
                      terminology, but you get what I mean).
                      >
                      def somefunc(param, _statics={}):
                      # code here
                      >
                      I haven't tried to create
                      private instance attributes, mainly because it would just be too
                      difficult,
                      >
                      Same recipe as above : single leading underscore.
                      >
                      and it would be awful syntax. I'm not considering actually
                      using this, but I do have a couple questions about it.
                      >
                      1. Has anyone else ever come up with something like this? I can't
                      imagine I'm the only person who's ever thought of this.
                      >
                      With something like trying to forcefit access restriction in Python ?
                      Nope, you're not the first one here. I've not seen anyone using such a
                      thing in any of the projects I've worked on/with yet.
                      You ignored certain parts of my post, like "I'm not considering
                      actually using this, but I do have a couple questions about it"; I
                      assume that means you're not even going to begin to attempt to answer
                      my queries. Likewise, you ignored a similar message on the page I
                      linked to. I already knew about everything you told me.

                      With all this active ignoring, I get the feeling you're not even
                      trying to respond to my questions. So why respond in the first place?
                      Were you trying to be hostile? Because I can't see any other intent
                      for your post.

                      Comment

                      • Bruno Desthuilliers

                        #12
                        Re: Private data

                        Dustan a écrit :
                        On Mar 19, 4:09 am, Bruno Desthuilliers <bruno.
                        42.desthuilli.. .@wtf.websitebu ro.oops.comwrot e:
                        >Dustan a écrit :
                        >>
                        >>http://dustangroups.googlepages.com/...ibutesinpython
                        >>This is something that I just threw together this morning, after a
                        >>eureka moment. It's a way of creating private class attributes
                        (snip)
                        >>1. Has anyone else ever come up with something like this? I can't
                        >>imagine I'm the only person who's ever thought of this.
                        >With something like trying to forcefit access restriction in Python ?
                        >Nope, you're not the first one here. I've not seen anyone using such a
                        >thing in any of the projects I've worked on/with yet.
                        >
                        You ignored certain parts of my post, like "I'm not considering
                        actually using this, but I do have a couple questions about it"; I
                        assume that means you're not even going to begin to attempt to answer
                        my queries. Likewise, you ignored a similar message on the page I
                        linked to. I already knew about everything you told me.
                        >
                        With all this active ignoring, I get the feeling you're not even
                        trying to respond to my questions. So why respond in the first place?
                        Were you trying to be hostile?
                        >
                        Because I can't see any other intent
                        for your post.
                        I didn't missed the "I'm not considering actually using this" part, but
                        I missed the note on the page - and I apologize if you felt offended by
                        my remarks, which were effectivly a bit on the reactive side. Still
                        there was an implied question : *why* trying to implement access
                        restriction in Python ?

                        Comment

                        • Bruno Desthuilliers

                          #13
                          Re: Private data

                          Dustan a écrit :May I report a small typo ?

                          """
                          def interanalDataDe corator(func):
                          """

                          Talking about private attributes... !-)

                          Comment

                          • Dustan

                            #14
                            Re: Private data

                            On Mar 19, 3:06 pm, Bruno Desthuilliers
                            <bdesth.quelque ch...@free.quel quepart.frwrote :
                            Dustan a écrit :
                            >>
                            May I report a small typo ?
                            Absolutely. I might even fix it.
                            """
                            def interanalDataDe corator(func):
                            """
                            >
                            Talking about private attributes... !-)
                            Thanks.

                            Comment

                            • Dustan

                              #15
                              Re: Private data

                              On Mar 19, 7:31 am, Bruno Desthuilliers <bruno.
                              42.desthuilli.. .@wtf.websitebu ro.oops.comwrot e:
                              Dustan a écrit :
                              >
                              >
                              >
                              On Mar 19, 4:09 am, Bruno Desthuilliers <bruno.
                              42.desthuilli.. .@wtf.websitebu ro.oops.comwrot e:
                              Dustan a écrit :
                              >
                              >>http://dustangroups.googlepages.com/...ibutesinpython
                              >This is something that I just threw together this morning, after a
                              >eureka moment. It's a way of creating private class attributes
                              (snip)
                              >1. Has anyone else ever come up with something like this? I can't
                              >imagine I'm the only person who's ever thought of this.
                              With something like trying to forcefit access restriction in Python ?
                              Nope, you're not the first one here. I've not seen anyone using such a
                              thing in any of the projects I've worked on/with yet.
                              >
                              You ignored certain parts of my post, like "I'm not considering
                              actually using this, but I do have a couple questions about it"; I
                              assume that means you're not even going to begin to attempt to answer
                              my queries. Likewise, you ignored a similar message on the page I
                              linked to. I already knew about everything you told me.
                              >
                              With all this active ignoring, I get the feeling you're not even
                              trying to respond to my questions. So why respond in the first place?
                              Were you trying to be hostile?
                              >
                              Because I can't see any other intent
                              for your post.
                              >
                              I didn't missed the "I'm not considering actually using this" part, but
                              I missed the note on the page - and I apologize if you felt offended by
                              my remarks, which were effectivly a bit on the reactive side.
                              Apology accepted.
                              Still
                              there was an implied question : *why* trying to implement access
                              restriction in Python ?
                              My curiosity gets piqued every once in a second or so. This just
                              happened to be one of those occasions. I assume you read my questions;
                              I was curious to see if anyone else had ever thought up something as
                              utterly twisted and conniving as this, and how one could go about
                              hacking into such a construct (I might not have said it explicitly,
                              but I was pretty sure that there had to be a way, because it just
                              wouldn't be python if there wasn't a way of getting into it).

                              Comment

                              Working...