How is this called? s = s.splitlines()

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?

assignment statement, no special name I know of for rebinding an existing name

As Terry says, it is just an “assignment”.

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.

Cheers,
Cameron Simpson cs@cskk.id.au

In python, values have types, while variables don’t (unless you use type annotations or something).

It’s dynamic typing.

I tend to avoid changing the type of a variable in Python, to make things easier for mypy or pylint or whatever.

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.

To learn more about the differences between strong/weak and dynamic/static typing, you can read What To Know Before Debating Type Systems.

1 Like

ok, thank you.

But let’s take another example:
x = x + 1
in programming language it is assignment, variable binding…

But in mathematics x = x+1 is non-sense.

The Functional programming paradigm is closer to mathematics. So how would a functional style of incrementing look like?

(btw. when speaking about incrementing, I would love to have something like inc(x) function that simply increments x.
Is there something like that?)

EDIT: I have created a separate thread about increment/decrement here: Increment, decrement... Can we have inc(), dec() functions in Python? feature request :)

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:

fib(0) = 1
fib(1) = 1
fib(n) = fib(n - 2) + fib(n - 1)

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.

There is an assortment of functional programming languages in existence.

In something like Lisp, you can increment a variable.

In a “pure functional” language, you don’t do that. Instead, you might make a recursive function call, incrementing a value as you go.

Python is actually somewhat close to Lisp, semantically - but far from it syntactically.

Well, of course you can parameterize a function by time.

Of course you can; but the most common equivalent of “assignment” that I’ve seen is subscripted variables, which is why that was the example I used.

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);.