Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assignment operator
#4
Assignment happens at places, where you don't expect it.
For example a for-loop is an assignment.
For each iteration the returned result from the iterator is assigned to the name.
When you create a class or a function, it's an assignment of the code block to a name.
The name is just a reference to the in memory living object.
An object can have many references (assigned to names).
The term variables is miss-leading.

a = 42
This code means, that a is now a reference to 42, which is the type int, which is in memory.
b = a
Now it's getting complicated. The name a is pointing to 42.
The name b is not pointing to a.
Instead it has the reference to object 42
If you change now the name a, b will still hold the reference to 42.
In other words, the assignment is by reference and not by value.

This is in Python a big pitfall.
There is another case, where you work with mutable types.
This means a value or many values can be changed on the object itself.
int, float, tuple, str, frozenset are immutable types, which could not change the value after creation.
The counter part are list, dict, set (and all other I forgot to name). They are mutable.
This means you can change the content of a list, dict, set etc. and all names, which have a reference to
the object which has still access to the mutated object.


It's very late, I do not have enough time to explain arithmetic operators with the right terms.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
Assignment operator - by DavidTheGrockle - Aug-08-2019, 08:47 PM
RE: Assignment operator - by jefsummers - Aug-08-2019, 08:57 PM
RE: Assignment operator - by ichabod801 - Aug-08-2019, 09:00 PM
RE: Assignment operator - by DeaD_EyE - Aug-09-2019, 01:00 AM
RE: Assignment operator - by DavidTheGrockle - Aug-10-2019, 08:05 PM
RE: Assignment operator - by perfringo - Aug-10-2019, 10:48 PM
RE: Assignment operator - by ichabod801 - Aug-10-2019, 08:13 PM
RE: Assignment operator - by ThomasL - Aug-11-2019, 07:16 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  assignment: not an operator nor expression, but x=y=z=3 works fine? jefdaels 1 2,290 Jan-29-2019, 02:19 PM
Last Post: perfringo
  How many variables/values are limited to single assignment operator? Prabakaran141 1 2,106 Sep-06-2018, 03:32 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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