-1

I'm using Python 3.x

Executed the below code and couldn't formulate a reason.

alist = [10,1000,100]
blist = alist

alist += [34]

print('alist after adding', alist) #this is reflected in blist too
print('change reflects in blist ', blist) 

alist = 35 #but this is not reflected in blist

print('alist after assigning ', alist)
print('change not reflected blist ', blist)

You can execute it at the below link

Click to Run

I couldn't find a justification, any pointers to find the root cause or your findings on it if you had came across it will be helpful.

Thanks

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
luckyluke
  • 642
  • 2
  • 7
  • 22
  • python is a loosely typed language. That's you have to keep in mind – Arpit Solanki Aug 01 '17 at 07:49
  • 1
    @ArpitSolanki No, Python is a strongly-typed language, however, it might _appear_ to be weakly-typed, because [Other languages have "variables", Python has "names"](http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables), and Python names do not carry type information. That information is held by the Python objects themselves. While it's _possible_ to change the type of a Python object, it's not particularly easy, and rarely useful. – PM 2Ring Aug 01 '17 at 07:56

2 Answers2

3

Here is what happens ...

  • alist = [10,1000,100] means alist references to an object of type list.
  • blist = alist means blist also points to the same object type as alist points i.e. a list.
  • alist += [34] adds 34 to alist and resultant will be in alist. alist is still referencing to an object of type list.
  • alist = 35 means you have changed the reference type of alist from list to a int whose value is 35 and not list anymore.
  • Doing so doesn't impact the actual list hence blist displays list and alist displays 35.

Use type(variable) to understand better, Check this...

alist = [10,1000,100]
blist = alist

alist += [34]
print("BEFORE: type of alist", type(alist))
print("BEFORE: type of blist", type(blist))

alist = 35 #but this is not reflected in blist
print("AFTER: type of alist", type(alist))
print("AFTER: type of blist", type(blist))

Sample Run

BEFORE: type of alist <class 'list'>
BEFORE: type of blist <class 'list'>
AFTER: type of alist <class 'int'>
AFTER: type of blist <class 'list'>
JRG
  • 4,037
  • 3
  • 23
  • 34
  • thumbs-up man, I forget that point, yes you're right, it incarnates into a int type – luckyluke Aug 01 '17 at 07:48
  • 1
    You can use `type(variable)` to know its type. Please accept the answer and vote up, if it has resolved your question :) – JRG Aug 01 '17 at 07:51
  • 1
    This is a very badly worded way of explaining *references*. Nothing “changes the datatype”. `alist = 35` makes the variable `alist` just refer to some other object which has nothing to do with the original list object. – poke Aug 01 '17 at 07:51
  • 3
    `alist = alist + [34]` is completely different from `alist += [34]`. The first changes the reference of `alist` whereas the latter doesn't. – Jean-François Fabre Aug 01 '17 at 07:57
  • 2
    You appear to have some misunderstandings about how Python's data model works. You, too, should take a look at [Other languages have "variables", Python has "names"](http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables). For more in-depth info, please see [Facts and myths about Python names and values](http://nedbatchelder.com/text/names.html), which was written by SO veteran Ned Batchelder. – PM 2Ring Aug 01 '17 at 07:59
  • @Jean-FrançoisFabre is it apt to say, the memory of the object is moved to a different location ? – luckyluke Aug 01 '17 at 09:38
2

The statement alist += [35] is shorthand for alist.extend([35]). See the docs for extend.

The variable alist is a reference to a list. This list will be appended with 35. Since the variable blist is a reference to the same list, the same happens there.

When you assign 35 to the variable alist, what alist references changes. It now points to this value and blist still references the list.

All the datastructures you create in your program are stored in the memory, and your variables are just references to those pieces of memory. Some operations will alter the data that is references in the memory, for example extend. Variable assignment will change what the variable points too.

bbnkttp
  • 264
  • 1
  • 11