473,385 Members | 1,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Reference?

I've searched all the Python docs I could find, but I haven't seen any
mention of referencing function arguments, such as you would with the
& in c/c++. Is this possible in Python?
Jul 18 '05 #1
5 1487
Kris Caselden wrote:
I've searched all the Python docs I could find, but I haven't seen any
mention of referencing function arguments, such as you would with the
& in c/c++. Is this possible in Python?


The analog of the Python way of handling args among the C-style languages is
Java. So, no, you are on mission impossible :-)

class Mutable:
pass

v1 = Mutable()
v1.name = "v1"
v2 = "v2" #strings are immutable

def fun(a1, a2):
a1.name = "a1"
a2 = "a2"
fun(v1, v2)

print v1.name # prints a1
print v2 # prints v2

As a workaround, you can do:

def fun2():
a1 = Mutable()
a1.name = "A1"
return a1, "A2"

v1, v2 = fun2()

print v1.name # prints A1
print v2 # prints A2

See the tutorial (http://www.python.org/doc/current/tut/node6.html) for the
tricks you *can* do with function arguments

Peter
Jul 18 '05 #2
Kris Caselden wrote:
I've searched all the Python docs I could find, but I haven't seen any
mention of referencing function arguments, such as you would with the
& in c/c++. Is this possible in Python?


The analog of the Python way of handling args among the C-style languages is
Java. So, no, you are on mission impossible :-)

class Mutable:
pass

v1 = Mutable()
v1.name = "v1"
v2 = "v2" #strings are immutable

def fun(a1, a2):
a1.name = "a1"
a2 = "a2"
fun(v1, v2)

print v1.name # prints a1
print v2 # prints v2

As a workaround, you can do:

def fun2():
a1 = Mutable()
a1.name = "A1"
return a1, "A2"

v1, v2 = fun2()

print v1.name # prints A1
print v2 # prints A2

See the tutorial (http://www.python.org/doc/current/tut/node6.html) for the
tricks you *can* do with function arguments

Peter
Jul 18 '05 #3

"Kris Caselden" <go****@hanger.snowbird.net> schrieb im Newsbeitrag
news:ab**************************@posting.google.c om...
I've searched all the Python docs I could find, but I haven't seen any
mention of referencing function arguments, such as you would with the
& in c/c++. Is this possible in Python?
Your question is probably not as sophisticated: Parameter passing in Python
is *always* like & in C++.

There had been an exaustive disyussion some weeks ago: here is my
contribution:

Sent: Wednesday, August 13, 2003 9:12 AM
Subject: Re: two quick questions

The following examples might clear the more theoretical elaborations .....
def noUse(a):
a=(4,5,6)

def tricky(a):
a[0]=(7,8,9)

# case 1
x=[1,2,3]
print x
tricky(x)

x=(1,2,3)
# case 2
noUse(x)
print x

# case 3
tricky([x])
print x

# case 4
y=[x]
tricky (y)
print x
print y[0]

# case 5
tricky(x)
print x
Kindly
Michael Peuser

Jul 18 '05 #4

"Kris Caselden" <go****@hanger.snowbird.net> schrieb im Newsbeitrag
news:ab**************************@posting.google.c om...
I've searched all the Python docs I could find, but I haven't seen any
mention of referencing function arguments, such as you would with the
& in c/c++. Is this possible in Python?
Your question is probably not as sophisticated: Parameter passing in Python
is *always* like & in C++.

There had been an exaustive disyussion some weeks ago: here is my
contribution:

Sent: Wednesday, August 13, 2003 9:12 AM
Subject: Re: two quick questions

The following examples might clear the more theoretical elaborations .....
def noUse(a):
a=(4,5,6)

def tricky(a):
a[0]=(7,8,9)

# case 1
x=[1,2,3]
print x
tricky(x)

x=(1,2,3)
# case 2
noUse(x)
print x

# case 3
tricky([x])
print x

# case 4
y=[x]
tricky (y)
print x
print y[0]

# case 5
tricky(x)
print x
Kindly
Michael Peuser

Jul 18 '05 #5
On Sat, Aug 23, 2003 at 01:57:33AM -0700, Kris Caselden wrote:
I've searched all the Python docs I could find, but I haven't seen any
mention of referencing function arguments, such as you would with the
& in c/c++. Is this possible in Python?


Python is pass-by-reference by default.
f = []
def foo(n): .... n.append("hi")
.... foo(f)
print f

['hi']

--
m a c k s t a n n mack @ incise.org http://incise.org
A bore is someone who persists in holding his own views after we have
enlightened him with ours.

Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: RU | last post by:
Hi, I am working on a porting project to port C/C++ application from unixware C++, AT&T Standard components to g++ with STL on Linux. This application has been working properly on...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
11
by: Doug | last post by:
Is there any harm in passing an object into a method with the 'ref' keyword if the object is already a reference variable? If not, is there any benefit?
13
by: Abe Frohnman | last post by:
Hello all, I'm passing a reference to a class into the constructor of a form, like so: public MyForm(int count, ref Area myArea) {...} How can I use myArea outside the constructor? Should I...
4
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then...
13
by: al.cpwn | last post by:
I get that these two are different int* get() { static int m; return &m; } int& get() {
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
27
by: David W | last post by:
I'm almost tearing my hair out. A colleague claimed that a null reference can exist, like this: void f( int& p ) { printf( "%d\n", p ); } int main (int argc, char *argv) {
41
by: Summercool | last post by:
Can we confirm the following? also someone said, Java also has "reference" like in C++, which is an "implicit pointer": Pointer and Reference --------------------- I am starting to see what...
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.