473,473 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Call by reference and address in Python

440 Contributor
Hi,

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

Thanks in advacne
PSB
Feb 23 '07 #1
10 2264
bartonc
6,596 Recognized Expert Expert
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:
Expand|Select|Wrap|Line Numbers
  1. def SomeFunction(listTypeArg):
  2.     listTypeArg[0] = 1
  3.  
  4. aList = [0, 0, 0]
  5.  
  6. SomeFunction(alist)
  7. 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.
Feb 23 '07 #2
Motoma
3,237 Recognized Expert Specialist
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.
Feb 23 '07 #3
cybervegan
36 New Member
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
Feb 23 '07 #4
bartonc
6,596 Recognized Expert Expert
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.
Feb 24 '07 #5
Motoma
3,237 Recognized Expert Specialist
I so agree. Thanks for the input.
Only a FOOL would see these as good points!
Feb 24 '07 #6
psbasha
440 Contributor
Only a FOOL would see these as good points!
Expand|Select|Wrap|Line Numbers
  1. >>> def f(l):
  2.     list[0]=2
  3.  
  4.  
  5. >>> a=[0,0,0]
  6. >>> f(a)
  7. >>> print a
  8. [0, 0, 0]
  9. >>> 
  10.  
I am not able to get the output as suggested in the discussion.Could anybody help me in solving this or umderstanding this problem
Feb 24 '07 #7
Motoma
3,237 Recognized Expert Specialist
>>> 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.Could 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.

Expand|Select|Wrap|Line Numbers
  1. >>> def f(l):
  2.     l[0] = 2
  3.  
  4. >>> a = [0, 0, 0]
  5. >>> f(a)
  6. >>> a
  7. [2, 0, 0]
  8.  
  9.  
Feb 24 '07 #8
bartonc
6,596 Recognized Expert Expert
Expand|Select|Wrap|Line Numbers
  1. >>> def f(l):
  2.     list[0]=2
  3.  
  4.  
  5. >>> a=[0,0,0]
  6. >>> f(a)
  7. >>> print a
  8. [0, 0, 0]
  9. >>> 
  10.  
I am not able to get the output as suggested in the discussion.Could 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.
Expand|Select|Wrap|Line Numbers
  1. >>> def f(l):
  2.     # list[0]=2 # you need to use I, to assign to Don't use "list" that word is reserved for creating lists.
  3.  
  4.     I[0]=2
  5.  
  6.  
  7. >>> a=[0,0,0]
  8. >>> f(a)
  9. >>> print a
  10. [0, 0, 0]
  11. >>> 
  12.  
Feb 24 '07 #9
psbasha
440 Contributor
You are making progress now! You have posted some code. This is what we are good at helping with here.
Expand|Select|Wrap|Line Numbers
  1. >>> def f(l):
  2.     # list[0]=2 # you need to use I, to assign to Don't use "list" that word is reserved for creating lists.
  3.  
  4.     I[0]=2
  5.  
  6.  
  7. >>> a=[0,0,0]
  8. >>> f(a)
  9. >>> print a
  10. [0, 0, 0]
  11. >>> 
  12.  
It is working fine

Thanks
PSB
Feb 25 '07 #10
bartonc
6,596 Recognized Expert Expert
It is working fine

Thanks
PSB
You are welcome.
Feb 25 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Chris S. | last post by:
I'm trying to make a graphical editor and browser for Pickled files. One aspect I'm not sure about is how to detect multiple references to the same data. For instance, say I had the Pickled...
36
by: Riccardo Rossi | last post by:
Hi all! How does Python pass arguments to a function? By value or by reference? Thanks, Riccardo Rossi.
35
by: hasho | last post by:
Why is "call by address" faster than "call by value"?
24
by: Jazper | last post by:
hi i have this problem. i made a class deverted by CRootItem with implementation of IDisposable-Interface. i made a test-funktion to test my Dispose-Method.... but when set a breakpoint in my...
8
by: ThomasR | last post by:
I understand that virtual methods on inherited objects are slower than non-virtual methods because of the indirection required to support the call. However, when looking at IL code produced by...
0
by: Jeremy | last post by:
Hi, I recently started using Python and am working on Python 2.3.6 on Redhat. I have developed a fat C++ extension for it and have the following problem: The main module is in the Python code...
3
by: sisqorap | last post by:
Hi all, I have this kind of warning and would like to have it solved: Warning: Call-time pass-by-reference has been deprecated; If you would like to pass it by reference, modify the declaration...
2
by: bappai | last post by:
Hello, I am trying to actually call a GUI from my C++ code which would have buttons and therefore can call functions from C++ again, ie extend the C++ code also. I am faced with a peculiar...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.