Hi,
Is Python supports Call by reference and Call by address in function or methods,without using C/C++ modules?.
Thanks in advacne
PSB
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: - 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.
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.
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
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.
Motoma 3,237
Recognized Expert Specialist
I so agree. Thanks for the input.
Only a FOOL would see these as good points!
Only a FOOL would see these as good points!
-
>>> 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
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. -
>>> def f(l):
-
l[0] = 2
-
-
>>> a = [0, 0, 0]
-
>>> f(a)
-
>>> a
-
[2, 0, 0]
-
-
bartonc 6,596
Recognized Expert Expert -
>>> 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
You are making progress now! You have posted some code. This is what we are good at helping with here. -
>>> 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]
-
>>>
-
You are making progress now! You have posted some code. This is what we are good at helping with here. -
>>> 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
bartonc 6,596
Recognized Expert Expert
It is working fine
Thanks
PSB
You are welcome.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
by: Riccardo Rossi |
last post by:
Hi all!
How does Python pass arguments to a function? By value or by reference?
Thanks,
Riccardo Rossi.
|
by: hasho |
last post by:
Why is "call by address" faster than "call by value"?
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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'
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |