hello,
I wonder how is this called?
Suppose I have string variable s.
I can call a method on this s and assign the result right to the original variable for example: s = s.split()
interestingly it works even when the method result is of different type, e.g.: s = s.splitlines() s will now be a list of course…
I just wonder how is this called?
When you have expression on one side, and the evaluation (result) is passed to the variable that was included in expression.
It must have some terminology in programming language theory, right?
All that means is that we’re storing a value in a variable. What you can
store depends on the language you’re using. In Python, values are
strongly typed, but variables (which are just references to values) are not typed. You can store a reference to any value in a variable.
A lot of dynamic languages work this way, and more static languages tend
to associate types with the variables themselves.
As a matter of practice, I personally tend to try to use a single type
with most variables so that there is less confusion when reading the
code. For example, your:
s = s.splitlines()
I would be likely to write as:
lines = s.splitlines()
There are some exceptions to this rule of thumb, and often None is
bundled in there with “a single type” as the “unset” value, even though None is of its own type.
This is called “assignment” or “name binding”. It binds a value to a name, or assigns a name to a value.
There is no other special term for it.
To learn more about names and values in Python, you can read this.
In some languages, variables (names) have types, and values are just a blob of bits. This is called static typing because the compiler can work out the type of expressions by looking at the variables used at compile-time, before the code runs.
In Python, and many other languages, variables are just names that can be bound to any value, but values are objects with a type. This is called dynamic typing because the type of expressions cannot be worked out at compile-time, only at runtime.
In some languages, the compiler or interpreter is very strict about enforcing rules about types. This is called “strong typing”. In other languages, the compiler or interpreter is not very strict, and only enforces a few rules, or none at all. This is called “weak typing”.
Python is a moderately strongly typed language: for example, it allows you to add an int to a float, but not an int to a string.
Mathematics has no concept of chronology. Something is either true or it isn’t. That’s why x = x + 1 makes no sense, because the value of x is constant.
Programming is more progressive or iterative. It’s more akin to a mathematician writing:
x3 = x2 + 1
indicating that the next value of x is higher than the previous. Mathematicians love to define rules for iteration that way, for instance:
which translates reasonably well into Python thus:
def fib(n):
match n:
case 0: return 1
case 1: return 1
case _: return fib(n - 2) + fib(n - 1)
But the idea of “incrementing a variable” makes no sense in mathematics without some mechanic like this.
So, go ahead and define incrementing in a way that mathematics would accept, and then you can translate that into Python; or, more likely, just write x += 1 and have done with it.
In a pure functional language, there is no incrementing, because assignment to an existing variable is a side-effect, and pure functional languages disallow side-effects.
So you would be able to take the value of x and add 1 to it, giving you a new value, and you could pass that new value to a function, but you can not assign it back to x.
Many functional programming languages, like Lisp and Python, are not pure, and so do allow side-effects, including variable assignment. So you already know the answer: they say x = x + 1 or equivalent, using whatever syntax they use for addition and assignment.
(Purists do not consider Python to be a good functional programming language, in either the language itself or the culture, but it is certainly at least a bit “functional programming-ish”. Lisp on the other hand may not be pure but it is certainly a FP language.)
I’m not a Lisp expert, but I think you can do this to assign 10 to x and then increment it:
(setq x 10) ;
(setq x (+ 1 x)) ;
The increment part (+ 1 x) has a short-cut (1+ x), and the second assignment also has a short-cut: (incf x);.