Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #26424 > unrolled thread
| Started by | Subhabrata <subhabangalore@gmail.com> |
|---|---|
| First post | 2012-08-03 04:49 -0700 |
| Last post | 2012-08-05 19:56 -0700 |
| Articles | 12 — 10 participants |
Back to article view | Back to comp.lang.python
Calling Values Subhabrata <subhabangalore@gmail.com> - 2012-08-03 04:49 -0700
Re: Calling Values Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-08-03 13:07 +0100
Re: Calling Values Nobody <nobody@nowhere.com> - 2012-08-03 13:44 +0100
Re: Calling Values Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-08-03 14:32 +0200
Re: Calling Values subhabangalore@gmail.com - 2012-08-03 07:38 -0700
RE: Calling Values "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-08-03 15:30 +0000
Re: Calling Values Ethan Furman <ethan@stoneleaf.us> - 2012-08-03 08:37 -0700
Re: Calling Values Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-03 16:24 +0000
Re: Calling Values Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-03 13:20 -0400
Re: Calling Values subhabangalore@gmail.com - 2012-08-03 11:23 -0700
Re: Calling Values subhabangalore@gmail.com - 2012-08-03 11:23 -0700
Re: Calling Values alex23 <wuwei23@gmail.com> - 2012-08-05 19:56 -0700
| From | Subhabrata <subhabangalore@gmail.com> |
|---|---|
| Date | 2012-08-03 04:49 -0700 |
| Subject | Calling Values |
| Message-ID | <8bdc29d5-fa88-4ead-a4a1-135d708eeb57@googlegroups.com> |
Dear Group, I am trying to call the values of one function in the another function in the following way: def func1(): num1=10 num2=20 print "The Second Number is:",num2 return def func2(): num3=num1+num2 num4=num3+num1 print "New Number One is:",num3 print "New Number Two is:",num4 return I am preferring not to use argument passing or using class? Is there any alternate way? Thanking in Advance, Regards, Subhabrata.
[toc] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2012-08-03 13:07 +0100 |
| Message-ID | <mailman.2896.1343995588.4697.python-list@python.org> |
| In reply to | #26424 |
On 03/08/2012 12:49, Subhabrata wrote: > Dear Group, > > I am trying to call the values of one function in the another function in the following way: > def func1(): > num1=10 > num2=20 > print "The Second Number is:",num2 > return > > def func2(): > num3=num1+num2 > num4=num3+num1 > print "New Number One is:",num3 > print "New Number Two is:",num4 > return > > I am preferring not to use argument passing or using class? Is there any alternate way? > > Thanking in Advance, > Regards, > Subhabrata. > I think you've got the wrong group, but I don't know the best one for psychiatrists :) Seriously I can't see what you're trying to achieve with this approach. Can you explain it and I'm certain that we'll come up with a decent solution to your problem, whatever that may be. -- Cheers. Mark Lawrence.
[toc] | [prev] | [next] | [standalone]
| From | Nobody <nobody@nowhere.com> |
|---|---|
| Date | 2012-08-03 13:44 +0100 |
| Message-ID | <pan.2012.08.03.12.44.44.600000@nowhere.com> |
| In reply to | #26424 |
On Fri, 03 Aug 2012 04:49:46 -0700, Subhabrata wrote: > I am trying to call the values of one function in the another function > in the following way: > def func1(): > num1=10 > num2=20 > print "The Second Number is:",num2 > return > > def func2(): > num3=num1+num2 > num4=num3+num1 A function's local variables only exist while that function is being executed[1]. It's meaningless to try to access them from outside the function. [1] There is an exception (closures), but it doesn't have any bearing on this particular problem.
[toc] | [prev] | [next] | [standalone]
| From | Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> |
|---|---|
| Date | 2012-08-03 14:32 +0200 |
| Message-ID | <p0tse9-3tp.ln1@satorlaser.homedns.org> |
| In reply to | #26424 |
Am 03.08.2012 13:49, schrieb Subhabrata: > I am trying to call the values of one function in the > another function in the following way: > > def func1(): > num1=10 > num2=20 > print "The Second Number is:",num2 > return > > def func2(): > num3=num1+num2 > num4=num3+num1 > print "New Number One is:",num3 > print "New Number Two is:",num4 > return > > I am preferring not to use argument passing or using class? You could make those variables global, see the "global" statement in the documentation of the language. However: I don't think that is a good idea and it will make your program more confusing to read than necessary, but go ahead and make that experience yourself. ;) If you want, you can post your code here when done so that others might give you hints how to do things easier and cleaner, like e.g. putting spaces around operators and using four spaces indention (See PEP 8) or dropping the implied return from functions that return nothing. Apart from that, the above code is too short and with too little info what it's supposed to achieve, I can't really give you better advise. Good luck! Uli
[toc] | [prev] | [next] | [standalone]
| From | subhabangalore@gmail.com |
|---|---|
| Date | 2012-08-03 07:38 -0700 |
| Message-ID | <21f0980d-10dc-4970-bace-8e909994fafd@googlegroups.com> |
| In reply to | #26424 |
On Friday, August 3, 2012 5:19:46 PM UTC+5:30, Subhabrata wrote:
> Dear Group,
>
>
>
> I am trying to call the values of one function in the another function in the following way:
>
> def func1():
>
> num1=10
>
> num2=20
>
> print "The Second Number is:",num2
>
> return
>
>
>
> def func2():
>
> num3=num1+num2
>
> num4=num3+num1
>
> print "New Number One is:",num3
>
> print "New Number Two is:",num4
>
> return
>
>
>
> I am preferring not to use argument passing or using class? Is there any alternate way?
>
>
>
> Thanking in Advance,
>
> Regards,
>
> Subhabrata.
Dear Group,
def func1():
num1=10
num2=20
print "The Second Number is:",num2
return
def func2():
func1()
num3=num1+num2
num4=num3+num1
print "New Number One is:",num3
print "New Number Two is:",num4
This works. Even you can incoportate some conditionals over func1() in func2() and run.
My question can I call its values of func1() too?
What it is the big deal in experimenting we may come up with some new code or a new need?
Indentation slightly differs while you post, I agree. Return I just like too use.
Mark you are too concerned for me, thanks.
Regards,
Subhabrata.
[toc] | [prev] | [next] | [standalone]
| From | "Prasad, Ramit" <ramit.prasad@jpmorgan.com> |
|---|---|
| Date | 2012-08-03 15:30 +0000 |
| Message-ID | <mailman.2908.1344007857.4697.python-list@python.org> |
| In reply to | #26439 |
> def func1():
>
> num1=10
>
> num2=20
>
> print "The Second Number is:",num2
>
> return
>
>
> def func2():
>
> func1()
> num3=num1+num2
>
> num4=num3+num1
>
> print "New Number One is:",num3
>
> print "New Number Two is:",num4
>
>
> This works. Even you can incoportate some conditionals over func1() in func2()
> and run.
This does not work. Python does not get "compiled" in the same manner
as other languages (C, Java etc). Since you never call func2(), there is no
error. Once you try calling func2() you will see it does not work. func1()
does work.
The Second Number is: 20
Traceback (most recent call last):
File "subha.py", line 24, in <module>
func2()
File "subha.py", line 15, in func2
num3=num1+num2
NameError: global name 'num1' is not defined
> My question can I call its values of func1() too?
> What it is the big deal in experimenting we may come up with some new code or
> a new need?
It is not a big deal, that is how you learn. You are just writing code that
neither works nor really shows enough to tell us why or what you are trying
to do. Not much I can do to guide or help you because I am completely lost
at your goal. The best I can do at the moment is say. func2 will not work.
You could return num1 and num2 from func1() and then it would work.
def func1():
num1=10
num2=20
print "The Second Number is:",num2
return num1, num2
def func2():
num1, num2 = func1()
num3=num1+num2
num4=num3+num1
print "New Number One is:",num3
print "New Number Two is:",num4
func2()
Ramit
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.
[toc] | [prev] | [next] | [standalone]
| From | Ethan Furman <ethan@stoneleaf.us> |
|---|---|
| Date | 2012-08-03 08:37 -0700 |
| Message-ID | <mailman.2909.1344007931.4697.python-list@python.org> |
| In reply to | #26439 |
subhabangalore@gmail.com wrote: > On Friday, August 3, 2012 5:19:46 PM UTC+5:30, Subhabrata wrote: >> Dear Group, >> >> >> >> I am trying to call the values of one function in the another function in the following way: >> >> def func1(): >> >> num1=10 >> >> num2=20 >> >> print "The Second Number is:",num2 >> >> return >> >> >> >> def func2(): >> >> num3=num1+num2 >> >> num4=num3+num1 >> >> print "New Number One is:",num3 >> >> print "New Number Two is:",num4 >> >> return >> >> >> >> I am preferring not to use argument passing or using class? Is there any alternate way? >> >> >> >> Thanking in Advance, >> >> Regards, >> >> Subhabrata. > > Dear Group, > > def func1(): > > num1=10 > > num2=20 > > print "The Second Number is:",num2 > > return > > > def func2(): > > func1() > num3=num1+num2 > > num4=num3+num1 > > print "New Number One is:",num3 > > print "New Number Two is:",num4 > > > This works. No, it doesn't. If it does work for you then you have code you aren't showing us. ~Ethan~
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2012-08-03 16:24 +0000 |
| Message-ID | <501bfb59$0$29978$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #26424 |
On Fri, 03 Aug 2012 04:49:46 -0700, Subhabrata wrote: > I am preferring not to use argument passing It's not 1964 any more and you're not programming in BASIC. What you have just told us is that you prefer not to write good quality programs, and that you prefer to write buggy, hard to maintain code. Of course that is your right to make such a foolish choice, but you shouldn't expect people to help you. There is enough terrible code in the world without encouraging people to write more. If you would like to learn good coding techniques, please stay around here and pay attention to modern practices for writing good quality code. If you would rather stick to worst-practices from the 1960s, don't expect any encouragement. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2012-08-03 13:20 -0400 |
| Message-ID | <mailman.2914.1344014492.4697.python-list@python.org> |
| In reply to | #26424 |
On Fri, 3 Aug 2012 04:49:46 -0700 (PDT), Subhabrata
<subhabangalore@gmail.com> declaimed the following in
gmane.comp.python.general:
> Dear Group,
>
> I am trying to call the values of one function in the another function in the following way:
Technically, "the values of one function" are whatever it RETURNS;
> def func1():
> num1=10
> num2=20
> print "The Second Number is:",num2
> return
>
This function returns None.
Recommended software design practices are that any thing inside the
function should be local to just that function -- a function should be a
black box -- you call it with some data, and you obtain some results
when it returns; what it does internally should be "invisible" and have
no effect on any other code.
Read:
http://en.wikipedia.org/wiki/Coupling_%28computer_programming%29
(what you are attempting falls into "content coupling" if you change the
use of "module" to "function")
However, Python lets you declare names to be global (to the
module/file). This is primarily meant to be used when a function must
rebind a module level entity. (This would be "common coupling")
def func1():
global num1, num2
...
But, as mentioned, that now makes num1 and num2 names that are known
outside the functions.
> def func2():
> num3=num1+num2
> num4=num3+num1
> print "New Number One is:",num3
> print "New Number Two is:",num4
> return
>
Misleading print statements, as you are NOT changing "number one" or
"number two"; you've just created two NEW names (num3, num4).
> I am preferring not to use argument passing or using class? Is there any alternate way?
>
Well, if you end func1 with
return num1, num2
you can change func2 into:
def func2():
n1, n2 = func1()
num3 = n1 + n2
num4 = num3 + n1
...
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [next] | [standalone]
| From | subhabangalore@gmail.com |
|---|---|
| Date | 2012-08-03 11:23 -0700 |
| Message-ID | <0ddd0d5d-1b56-4eed-8f27-f01a54c7ced6@googlegroups.com> |
| In reply to | #26450 |
On Friday, August 3, 2012 10:50:52 PM UTC+5:30, Dennis Lee Bieber wrote: > On Fri, 3 Aug 2012 04:49:46 -0700 (PDT), Subhabrata > > <subhabangalore@gmail.com> declaimed the following in > > gmane.comp.python.general: > > > > > Dear Group, > > > > > > I am trying to call the values of one function in the another function in the following way: > > > > Technically, "the values of one function" are whatever it RETURNS; > > > > > def func1(): > > > num1=10 > > > num2=20 > > > print "The Second Number is:",num2 > > > return > > > > > This function returns None. > > > > Recommended software design practices are that any thing inside the > > function should be local to just that function -- a function should be a > > black box -- you call it with some data, and you obtain some results > > when it returns; what it does internally should be "invisible" and have > > no effect on any other code. > > > > Read: > > http://en.wikipedia.org/wiki/Coupling_%28computer_programming%29 > > (what you are attempting falls into "content coupling" if you change the > > use of "module" to "function") > > > > However, Python lets you declare names to be global (to the > > module/file). This is primarily meant to be used when a function must > > rebind a module level entity. (This would be "common coupling") > > > > def func1(): > > global num1, num2 > > ... > > > > But, as mentioned, that now makes num1 and num2 names that are known > > outside the functions. > > > > > def func2(): > > > num3=num1+num2 > > > num4=num3+num1 > > > print "New Number One is:",num3 > > > print "New Number Two is:",num4 > > > return > > > > > Misleading print statements, as you are NOT changing "number one" or > > "number two"; you've just created two NEW names (num3, num4). > > > > > I am preferring not to use argument passing or using class? Is there any alternate way? > > > > > > > Well, if you end func1 with > > > > return num1, num2 > > > > you can change func2 into: > > > > def func2(): > > n1, n2 = func1() > > num3 = n1 + n2 > > num4 = num3 + n1 > > ... > > -- > > Wulfraed Dennis Lee Bieber AF6VN > > wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/ Dear Group, Absolutely brilliant, Ramit. Dennis also came with almost same answer. Using global may not give clean results everytime. I wanted to say, >>> def func1(): num1=10 num2=20 print "The Second Number is:",num2 >>> def func2(): func1() num3=50 print "The New Number is:",num3 >>> func2() The Second Number is: 20 The New Number is: 50 The post went slightly wrong sorry. No, I experiment myself on idle evenings to experiment with coding etc so I think of problems, practice on them and try to see if any better code evolves. Nothing else. I posted and Steve did not comment perhaps never happens. He rebukes me so much from my early days here, I just enjoy it. Regards and best wishes, Subhabrata.
[toc] | [prev] | [next] | [standalone]
| From | subhabangalore@gmail.com |
|---|---|
| Date | 2012-08-03 11:23 -0700 |
| Message-ID | <mailman.2915.1344018241.4697.python-list@python.org> |
| In reply to | #26450 |
On Friday, August 3, 2012 10:50:52 PM UTC+5:30, Dennis Lee Bieber wrote: > On Fri, 3 Aug 2012 04:49:46 -0700 (PDT), Subhabrata > > <subhabangalore@gmail.com> declaimed the following in > > gmane.comp.python.general: > > > > > Dear Group, > > > > > > I am trying to call the values of one function in the another function in the following way: > > > > Technically, "the values of one function" are whatever it RETURNS; > > > > > def func1(): > > > num1=10 > > > num2=20 > > > print "The Second Number is:",num2 > > > return > > > > > This function returns None. > > > > Recommended software design practices are that any thing inside the > > function should be local to just that function -- a function should be a > > black box -- you call it with some data, and you obtain some results > > when it returns; what it does internally should be "invisible" and have > > no effect on any other code. > > > > Read: > > http://en.wikipedia.org/wiki/Coupling_%28computer_programming%29 > > (what you are attempting falls into "content coupling" if you change the > > use of "module" to "function") > > > > However, Python lets you declare names to be global (to the > > module/file). This is primarily meant to be used when a function must > > rebind a module level entity. (This would be "common coupling") > > > > def func1(): > > global num1, num2 > > ... > > > > But, as mentioned, that now makes num1 and num2 names that are known > > outside the functions. > > > > > def func2(): > > > num3=num1+num2 > > > num4=num3+num1 > > > print "New Number One is:",num3 > > > print "New Number Two is:",num4 > > > return > > > > > Misleading print statements, as you are NOT changing "number one" or > > "number two"; you've just created two NEW names (num3, num4). > > > > > I am preferring not to use argument passing or using class? Is there any alternate way? > > > > > > > Well, if you end func1 with > > > > return num1, num2 > > > > you can change func2 into: > > > > def func2(): > > n1, n2 = func1() > > num3 = n1 + n2 > > num4 = num3 + n1 > > ... > > -- > > Wulfraed Dennis Lee Bieber AF6VN > > wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/ Dear Group, Absolutely brilliant, Ramit. Dennis also came with almost same answer. Using global may not give clean results everytime. I wanted to say, >>> def func1(): num1=10 num2=20 print "The Second Number is:",num2 >>> def func2(): func1() num3=50 print "The New Number is:",num3 >>> func2() The Second Number is: 20 The New Number is: 50 The post went slightly wrong sorry. No, I experiment myself on idle evenings to experiment with coding etc so I think of problems, practice on them and try to see if any better code evolves. Nothing else. I posted and Steve did not comment perhaps never happens. He rebukes me so much from my early days here, I just enjoy it. Regards and best wishes, Subhabrata.
[toc] | [prev] | [next] | [standalone]
| From | alex23 <wuwei23@gmail.com> |
|---|---|
| Date | 2012-08-05 19:56 -0700 |
| Message-ID | <c79b72cd-0295-4c9e-872e-fa929591e76f@sn4g2000pbc.googlegroups.com> |
| In reply to | #26424 |
On Aug 3, 9:49 pm, Subhabrata <subhabangal...@gmail.com> wrote: > I am preferring not to use argument passing or using class? Is there any alternate way? If you don't want to program in Python, don't use Python.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web