Variable, Object and Value

In Python everything is an object.
To start with, you need to remember few things:
1) Variables, functions and even code are object.
2) Every object has an ID, type and value.

ID stands for identification of a particular instance of an object. This ID can not change in the life time of that object.
3) Type identifies a class of an object. It can not change for the life of object.
4) Value is the content of the object and mutable objects can only change value. Immutable objects can not change the value.
5) Every variable in Python is a first class object. What looks like a simple variable actually is something more complex.
Let us see what these terms mean.


#!/usr/bin/python3
def main():
    x = 1
    print(x)
    print(id(x))
    print(type(x))
    x = 2
    print(x)
    print(id(x))
    print(type(x))
    x = 1
    print(x)
    print(id(x))
    print(type(x))

if __name__ == "__main__":
    main()

1
10455040
class ‘int’
2
10455072
class ‘int’
1
10455040
class ‘int’

As you see, changing values of x does not affect the immutable objects and the unique identifier of object ‘1’ remains same. What has been changed is simply the reference of the variable. First, we referred 1 (immutable integer object) to x (variable), and then change it. ID and type remain same.
Remember, numbers, strings and tuples are immutable. Lists, Dictionaries and other objects are mutable (changeable) but it depends.
Let us see a very brief example where it is explained in the comment section. The output is given along with.


#!/usr/bin/python3
# in python everything is object
# a variable is a reference to an object
# each object has an identity or an ID
x = 1
print(type(x))
print(id(x))
##################
# class 'int'
# 139113568
##################
# number, string, tuple -> immutable
# list, dictionary -> mutable
x = 1
y = 1
print(type(x))
print(id(x))
print(type(y))
print(id(y))
if x == y:
    print("True")
else:
    print("False")
if x is y:
    print("True")
else:
    print("False")
##################
# see the last two lines, both are true
# class 'int'
# 139113568
# class 'int'
# 139113568
# True
# True
##################
a = dict(x = 1, y = 1)
print(type(a))
print(id(a))
b = dict(x = 1, y = 1)
print(id(b))
if a == b:
    print("True")
else:
    print("False")
if a is b:
    print("True")
else:
    print("False")
##################
    # see the last two lines, one is true but the id is not same so it is false
# class 'dict'
# 3072650252
# 3072692524
# True
# False
##################
for i in range(0, 3):
    print(i, "=", id(i))
##################
# 0 = 139113552
# 1 = 139113568
# 2 = 139113584
##################

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s