Rant (was Re: x*x if x>10

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

    #16
    Re: Rant (was Re: x*x if x>10

    On Sun, 27 Jul 2008 19:46:32 +0200, "Diez B. Roggisch" <deets@nospam.w eb.de>
    wrote:

    >As a rule of thumb, don't return objects you didn't create inside a
    >function from scratch.
    I wish I'd had that advice when I started learning python. It would have
    saved me no end of grief.

    DaveM

    Comment

    • Terry Reedy

      #17
      Re: Rant (was Re: x*x if x&gt;10



      DaveM wrote:
      On Sun, 27 Jul 2008 19:46:32 +0200, "Diez B. Roggisch" <deets@nospam.w eb.de>
      wrote:
      >As a rule of thumb, don't return objects you didn't create inside a
      >function from scratch.
      Unless its job is specifically to get/fetch an object (reference
      thereto) from someplace the caller cannot or should not access. But
      then it should probably not mutate the object before returning the
      reference.
      >
      I wish I'd had that advice when I started learning python. It would have
      saved me no end of grief.
      I wish I had seen this 'rule' before too.

      Comment

      • Diez B. Roggisch

        #18
        Re: Rant (was Re: x*x if x&gt;10

        Terry Reedy schrieb:
        >
        >
        DaveM wrote:
        >On Sun, 27 Jul 2008 19:46:32 +0200, "Diez B. Roggisch"
        ><deets@nospam. web.de>
        >wrote:
        >>As a rule of thumb, don't return objects you didn't create inside a
        >>function from scratch.
        >
        Unless its job is specifically to get/fetch an object (reference
        thereto) from someplace the caller cannot or should not access. But
        then it should probably not mutate the object before returning the
        reference.
        I maybe should paraphrase "don't return objects you passed as arguments
        from a function". Of course there are exceptions to this rule - but
        these are few, the canonical being chained function calls like this:


        class Whatever(object ):

        def do_something(se lf, arguments):
        ....
        return self



        Diez

        Comment

        • greg

          #19
          Re: Rant (was Re: x*x if x&gt;10

          Diez B. Roggisch wrote:
          I maybe should paraphrase "don't return objects you passed as arguments
          from a function".
          The important thing is that a function shouldn't modify
          any object unless it's the express purpose of the function
          to do so.

          You could call this the "look but don't touch" rule.

          --
          Greg

          Comment

          Working...