why cannot assign to function call

S

scsoce

I have a function return a reference, and want to assign to the
reference, simply like this: return a
b = 0
* f( b ) = 1*
but the last line will be refused as "can't assign to function call".
In my thought , the assignment is very nature, but why the interpreter
refused to do that ?

thks
 
R

r

I have a function return a reference, and want to assign to the
reference, simply like this:
 >>def f(a)
          return a
     b = 0
    * f( b ) = 1*
but the last line will be refused as "can't assign to function call".
In my thought , the assignment is very nature,  but  why the interpreter
refused to do that ?

thks

because all you need to do is
trying to change the return value of a function before you even "know
what it is" so to speak, defeats the whole purpose of sending it there
in the first place. just assign the variable to a new value. Thats my
common sense answer, maybe somebody can give a technical one :)
 
B

Bruno Desthuilliers

scsoce a écrit :
I have a function return a reference, and want to assign to the
reference,

You have a function that returns an object. You can't "assign" to an
object - this makes no sense.

I'm afraid you are confusing Python's name/object bindings with C
pointers or C++ references.
 
J

John Machin

I have a function return a reference,

Stop right there. You don't have (and can't have, in Python) a
function which returns a reference that acts like a pointer in C or C+
+. Please tell us what manual, tutorial, book, blog or Usenet posting
gave you that idea, and we'll get the SWAT team sent out straight
away.
and want to assign to the
reference, simply like this:
 >>def f(a)
          return a

That's not a very useful function, even after you fix the syntax error
in the def statement. Would you care to give us a more realistic
example of what you are trying to achieve?
     b = 0
    * f( b ) = 1*

Is the * at the start of the line meant to indicate pointer
dereferencing like in C? If not, what is it? Why is there a * at the
end of the line?
but the last line will be refused as "can't assign to function call".
In my thought , the assignment is very nature,

Natural?? Please tell us why you would want to do that instead of:

b = 1
 but  why the interpreter
refused to do that ?

Because (the BDFL be praised!) it (was not, is not, will not be) in
the language grammar.
 
A

anthony.tolle

I have a function return a reference, and want to assign to the
reference, simply like this:
 >>def f(a)
          return a
     b = 0
    * f( b ) = 1*
but the last line will be refused as "can't assign to function call".
In my thought , the assignment is very nature,  but  why the interpreter
refused to do that ?

thks

Probably the closest thing you are going to get in Python would be the
following:
.... pass
........ return a
....1

But as others have pointed out, Python is not C/C++, and shouldn't be
treated as such.
 
A

Aaron Brady

I have a function return a reference, and want to assign to the
reference, simply like this:
 >>def f(a)
          return a
     b = 0
    * f( b ) = 1*
but the last line will be refused as "can't assign to function call".
In my thought , the assignment is very nature,  but  why the interpreter
refused to do that ?

'Why' is a long question. The syntax has advantages and disadvantages
(pros and cons), which weigh different amounts in different
languages. In Python, the cons weigh more. In C, the pros weigh
more. The short answer is, there is no such thing as assigning to
objects, only to variables.

You are talking like it could save you ten lines of code or something.
 
A

Aaron Brady

Perhaps we can send the the Pennsylvania State University out after
them.  I don't know why, but some fairly substantial people here are
scared of the PSU.

...

Oh, I have just been informed by my captors that the are the Python
Secre....

--Why would he take the time to carve "ARGHHH!"?
--Maybe he was dictating.
 
T

Terry Reedy

John said:
Stop right there. You don't have (and can't have, in Python) a
function which returns a reference that acts like a pointer in C or C+
+. Please tell us what manual, tutorial, book, blog or Usenet posting
gave you that idea,

Perhaps the ones claiming that Python is 'call by reference' and hence,
by implication, at least, 'return by reference'.
and we'll get the SWAT team sent out straight away.

I and others have posted many times that such a viewpoints leads to
confusion, such as in this post.

tjr
 
F

Fuzzyman

Stop right there. You don't have (and can't have, in Python) a
function which returns a reference that acts like a pointer in C or C+
+. Please tell us what manual, tutorial, book, blog or Usenet posting
gave you that idea, and we'll get the SWAT team sent out straight
away.


That's not a very useful function, even after you fix the syntax error
in the def statement. Would you care to give us a more realistic
example of what you are trying to achieve?


Is the * at the start of the line meant to indicate pointer
dereferencing like in C? If not, what is it? Why is there a * at the
end of the line?


Natural?? Please tell us why you would want to do that instead of:

    b = 1


Because (the BDFL be praised!) it (was not, is not, will not be) in
the language grammar.

Although not being able to do the following has on occasion annoyed
me:

f(x) += 1

If the object returned by f(x) supports in place operations then it is
an entirely logical meaning, but not the interpreter can't know ahead
of time whether that is the case or not.

Michael Foord
 
M

MRAB

Fuzzyman said:
Although not being able to do the following has on occasion annoyed
me:

f(x) += 1

If the object returned by f(x) supports in place operations then it is
an entirely logical meaning, but not the interpreter can't know ahead
of time whether that is the case or not.
+= always rebinds, even for in-place operations, so, in a sense, it's
not surprising!

I suppose it's like forbidding assignment within an expression (such as
an if- or while-condition): annoying sometimes, but a reasonable
restriction.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top