Python Forum
an error --> a wanted feature
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
an error --> a wanted feature
#1
today i had a "thinking-glitch" writing some code and got the error message SyntaxError: can't assign to function call.  i had coded a function call where i should have coded a dictionary reference.  so the error message is extly correct.  but, it got me to thinking.

there was a language i programmed in, in the past, that support this, and it was more the just a function call in the LHS (for example to return a value used as a subscript value).  it was a function call as the actual l-value.  the language had a special function call interface where the function, when called this way, could see that it was called this way, an got the value being assigned to it.  it was a language that passed arguments by reference, so the called function could assign to its arguments.  so they did stuff like  sin(x) = y in place of x = cosin(y).  the big debate in that language was "x*x = y" to fo "x = sqrt(y)" though it did no that.  the sin() was just implement to know what to do.

i'd like to see part of this, just the assignment to function call, implemented in Python.  when such an assignment was done, if the function definition did not support this, it would be an exception.  if it did support it, the function would be called, and passed the assigned value.  the syntax could be like:

def putstring(self,x) assigned s:
    self.array[x] = s
    return
this could be especially useful with classes.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
What is the name of the programming language which can do: x*x = y
Reply
#3
It might not be the one he intends, but Prolog offers similar behavior.
Reply
#4
Thanks for the update.
Reply
#5
(Nov-22-2017, 04:32 PM)heiner55 Wrote: What is the name of the programming language which can do: x*x = y

it couldn't actually do that ... but the debate was that since * was workable as a function, that is, you defined a function for each operator in type definitions for new programmer defined types, the existing definition of int and float (as we call them in python) should support __multiply__(x,x) = y so that x*x = y would work.  the existing implementations of the base types apparently were not in the language itself, but were part of the language interpreter

i cannot remember the name of the language.  something makes me think it was PCL or CLU.  it could have been ProLog (as micseydel hinted).  it had a discussion mailing list around 1984.  i didn't have access to a working deployment.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
Thanks
Reply
#7
Well, you can rewrite __mul__ method to do whatever you want  Smile That is a feature of Python I really love!
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
@wavic, you can't have it cause an assignment to the RHS.
Reply
#9
(Nov-23-2017, 09:18 AM)wavic Wrote: Well, you can rewrite __mul__ method to do whatever you want  Smile That is a feature of Python I really love!

lots of languages have this capability.  the Pike language has it.  i've read about it in other languages.  details, like the names of function will vary.
 
(Nov-23-2017, 04:51 PM)micseydel Wrote: @wavic, you can't have it cause an assignment to the RHS.

this was about assignment to the LHS, where sin(x) or __mul__(x,x) or x*x would be.  the idea was that the sin() function would handle this by handling an assigned-to case (i don't remember how that was detected) by doing a reverse function and assigning to it's argument.  Python could not easily do this, but that other language could because references were passed in all function call cases, even for types like int and float.  so it could assign to any argument as readily as Python can replace an element of a list.  for the multiply case, it would need to also detect that the two arguments were the same variable (not just the same value) and assign the square root of the assigned value, making the function/operation operate in reverse.  if they were not the same ... think about how you would handle x*y = z.

for Python this would be limited to not being able to assign to arguments.  but for certain things it might make more sense to get a more important value that way.  for any device that would be operated on primarily with one value (with a tuple in rare more complex cases). this could make sense for.  but it can also be argued that all cases can be handled by regular function calls, so it is unnecessary.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#10
Ruby has a feature that seems similar, where you can pass a block to a function. Not an annonymous function, but an actual block. Then, within the function, you can yield to that block. It's sort of like our iterators/generators, but is used for a whole lot of neat looking magic (like emulating with). http://ruby-doc.com/docs/ProgrammingRuby...rs.html#UD

def threeTimes
  yield
  yield
  yield
end
threeTimes { puts "Hello" }
produces:
Output:
Hello Hello Hello
Ok, I guess that's not really similar, it's just the first thing I thought of when it comes to functions doing different things depending on what happens "after" you call it.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020