Call by reference and address in Python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • psbasha
    Contributor
    • Feb 2007
    • 440

    #1

    Call by reference and address in Python

    Hi,

    Is Python supports Call by reference and Call by address in function or methods,without using C/C++ modules?.

    Thanks in advacne
    PSB
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by psbasha
    Hi,

    Is Python supports Call by reference and Call by address in function or methods,without using C/C++ modules?.

    Thanks in advacne
    PSB
    Almost everything in python is pass by reference. If you:
    Code:
    def SomeFunction(listTypeArg):
        listTypeArg[0] = 1
    
    aList = [0, 0, 0]
    
    SomeFunction(alist)
    print aList
    You get:
    [1, 0, 0]

    You can not, however, get the address of aList as you can in C. The beauty of python is that we never worry about where a variable is or what it's lifespan is. It's all taken care of.

    Comment

    • Motoma
      Recognized Expert Specialist
      • Jan 2007
      • 3236

      #3
      Originally posted by bartonc
      You can not, however, get the address of aList as you can in C. The beauty of python is that we never worry about where a variable is or what it's lifespan is. It's all taken care of.
      Beauty, or HACKER'S NIGHTMARE?!

      I'll leave you to ponder.

      Comment

      • cybervegan
        New Member
        • Jan 2007
        • 36

        #4
        Originally posted by Motoma
        Beauty, or HACKER'S NIGHTMARE?!

        I'll leave you to ponder.
        It's a beauty, because it means that it's far less likely that you get any of the following by writing valid python code:

        1) memory leaks (where memory is not deallocated when no longer needed -this doesn't include memory leaks from C/C++ modules)
        2) double-frees (freeing an already free'd memory block)
        3) dangling pointers (pointers that point to deallocated memory)
        4) orphaned memory (memory allocated, but with no pointers amed at it)
        5) mangled pointers (pointers that point to the wrong bit of memory)

        and probably some more too.

        If a program *needs* to access memory and pointers directly, it's the wrong problem domain for python. The only memory management problem I can think of that python exhibits (and then only by bad design) is cyclic references, where A refers to B which refers to A. Without care, deleting (and thus reclaiming the memory of) A or B is potentially problematic - you have to remember to break the reference to A from B before destroying A.

        It all comes down to choosing the right tool for the job.

        :-D cybervegan

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by cybervegan
          It's a beauty, because it means that it's far less likely that you get any of the following by writing valid python code:

          1) memory leaks (where memory is not deallocated when no longer needed -this doesn't include memory leaks from C/C++ modules)
          2) double-frees (freeing an already free'd memory block)
          3) dangling pointers (pointers that point to deallocated memory)
          4) orphaned memory (memory allocated, but with no pointers amed at it)
          5) mangled pointers (pointers that point to the wrong bit of memory)

          and probably some more too.

          If a program *needs* to access memory and pointers directly, it's the wrong problem domain for python. The only memory management problem I can think of that python exhibits (and then only by bad design) is cyclic references, where A refers to B which refers to A. Without care, deleting (and thus reclaiming the memory of) A or B is potentially problematic - you have to remember to break the reference to A from B before destroying A.

          It all comes down to choosing the right tool for the job.

          :-D cybervegan
          I so agree. Thanks for the input.

          Comment

          • Motoma
            Recognized Expert Specialist
            • Jan 2007
            • 3236

            #6
            Originally posted by bartonc
            I so agree. Thanks for the input.
            Only a FOOL would see these as good points!

            Comment

            • psbasha
              Contributor
              • Feb 2007
              • 440

              #7
              Originally posted by Motoma
              Only a FOOL would see these as good points!
              Code:
              >>> def f(l):
              	list[0]=2
              
              	
              >>> a=[0,0,0]
              >>> f(a)
              >>> print a
              [0, 0, 0]
              >>>
              I am not able to get the output as suggested in the discussion.Coul d anybody help me in solving this or umderstanding this problem
              Last edited by bartonc; Feb 24 '07, 07:27 PM. Reason: added [code][/code] tags

              Comment

              • Motoma
                Recognized Expert Specialist
                • Jan 2007
                • 3236

                #8
                Originally posted by psbasha
                >>> def f(l):
                list[0]=2


                >>> a=[0,0,0]
                >>> f(a)
                >>> print a
                [0, 0, 0]
                >>>

                I am not able to get the output as suggested in the discussion.Coul d anybody help me in solving this or umderstanding this problem
                Well, what you did here, was you passed a list as l, and then you set the value of list (not l) to 2.

                Code:
                >>> def f(l):
                	l[0] = 2
                
                >>> a = [0, 0, 0]
                >>> f(a)
                >>> a
                [2, 0, 0]

                Comment

                • bartonc
                  Recognized Expert Expert
                  • Sep 2006
                  • 6478

                  #9
                  Originally posted by psbasha
                  Code:
                  >>> def f(l):
                  	list[0]=2
                  
                  	
                  >>> a=[0,0,0]
                  >>> f(a)
                  >>> print a
                  [0, 0, 0]
                  >>>
                  I am not able to get the output as suggested in the discussion.Coul d anybody help me in solving this or umderstanding this problem
                  You are making progress now! You have posted some code. This is what we are good at helping with here.
                  Code:
                  >>> def f(l):
                  	# list[0]=2 # you need to use I, to assign to Don't use "list" that word is reserved for creating lists.
                  
                  	I[0]=2
                  
                  	
                  >>> a=[0,0,0]
                  >>> f(a)
                  >>> print a
                  [0, 0, 0]
                  >>>

                  Comment

                  • psbasha
                    Contributor
                    • Feb 2007
                    • 440

                    #10
                    Originally posted by bartonc
                    You are making progress now! You have posted some code. This is what we are good at helping with here.
                    Code:
                    >>> def f(l):
                    	# list[0]=2 # you need to use I, to assign to Don't use "list" that word is reserved for creating lists.
                    
                    	I[0]=2
                    
                    	
                    >>> a=[0,0,0]
                    >>> f(a)
                    >>> print a
                    [0, 0, 0]
                    >>>
                    It is working fine

                    Thanks
                    PSB

                    Comment

                    • bartonc
                      Recognized Expert Expert
                      • Sep 2006
                      • 6478

                      #11
                      Originally posted by psbasha
                      It is working fine

                      Thanks
                      PSB
                      You are welcome.

                      Comment

                      Working...