0% found this document useful (0 votes)
33 views

Week4 Class1

This document discusses key concepts in Python including dynamic typing, variables and objects, garbage collection, shared references, and equality. It explains that in Python types are determined at runtime, variables refer to objects, objects are garbage collected when no longer referenced, multiple variables can reference the same object, and objects can be modified in-place through shared references.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Week4 Class1

This document discusses key concepts in Python including dynamic typing, variables and objects, garbage collection, shared references, and equality. It explains that in Python types are determined at runtime, variables refer to objects, objects are garbage collected when no longer referenced, multiple variables can reference the same object, and objects can be modified in-place through shared references.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Agenda

• Dynamic Typing
• Variable, Objects and references
• Garbage collection
• Shared references and in-place changes
• Equality of objects
Dynamic Typing:

• In Python, types are determined automatically at runtime, not in response to declarations in


your code.

Variables, Objects, and References:

What happens when you do a=5??


• Variable creation: A variable (i.e., name), like a, is created when your code first assigns it a value. Future
assignments change the value of the already created name.
• Variable Types: A variable never has any type information or constraints associated with it. The notion of type
lives with objects, not names. Variables are generic in nature; they always simply refer to a particular object at a
particular point in time.
• Variable use: When a variable appears in an expression, it is immediately replaced with the object that it
currently refers to, whatever that may be. Further, all variables must be explicitly assigned before they can be
used; referencing unassigned variables results in errors
3 step process of variable assignment :
1. Create an object to represent the value 5.
2. Create the variable a, if it does not yet exist.
3. Link the variable a to the new object 5.

• Variables are entries in a system table, with spaces for links to objects.
• Objects are pieces of allocated memory, with enough space to represent the values for which they stand.(Each
object also has two standard header fields: a type designator used to mark the type of the object, and a reference
counter used to determine when it’s OK to reclaim the object)
• References are automatically followed pointers from variables to objects.
Objects Are Garbage-Collected:
>> a=5
>> a =‘anoop’
What happens to object 5??
• In Python, whenever a name is assigned to a new object, the space held by the prior object is reclaimed if it is not referenced
by any other name or object. This automatic reclamation of objects’ space is known as garbage collection
• Internally, Python accomplishes this by keeping a counter in every object that keeps track of the number of references
currently pointing to that object. As soon as (and exactly when) this counter drops to zero, the object’s memory space is
automatically reclaimed.

Shared references:
>> a = 3
>> b =a
• This scenario in Python—with multiple names referencing the same object—is usually called a shared reference

• The names a and b are not linked to each other directly when this happens

• There is no way to ever link a variable to another variable in Python. Rather, both variables point to the same
object via their references.

>>> a = 3

>>> b = a

>>> a = 'spam’
Shared References and In-Place Changes:
Python’s mutable types, including lists, dictionaries, and sets. For instance, an assignment to an offset in a list actually
changes the list object itself in place, rather than generating a brand-new list object.
>>> L1 = [2, 3, 4]
>>> L2 = L1
>>> L1 = 24

>>> L1 = [2, 3, 4] # A mutable object


>>> L2 = L1 # Make a reference to the same object
>>> L1[0] = 24 # An in-place change
Copy Objects:
>>> L2 = L1[:] # Make a copy of L1 (or list(L1), copy.copy(L1), etc.)
• to copy a dictionary or set, instead use their X.copy() method call
• import copy
X = copy.copy(Y) # Make top-level "shallow" copy of any object Y
X = copy.deepcopy(Y) # Make deep copy of any object Y: copy all nested parts
Shared References and Equality:
• Because of Python’s reference model, there are two different ways to check for equality in a Python program.
>>> L = [1, 2, 3]
>>> M = L # M and L reference the same object
>>> L == M # Same values
True
>>> L is M # Same objects
True

>>> L = [1, 2, 3]
>>> M = [1, 2, 3] # M and L reference different objects
>>> L == M # Same values
True
>>> L is M # Different objects
False
Shared References and Equality:
>>> X = 42
>>> Y = 42 # Should be two different objects
>>> X == Y
True
>>> X is Y # Same object anyhow: caching at work!
True
• Because small integers and strings are cached and reused, though, is tells us they reference the same
single object.
How to check number of references??
>>> import sys
>>> sys.getrefcount(1)

You might also like