2. Dynamic Typing Interlude

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What is a reference in Python?

- It is a link between variables and objects - An association, implemented as a pointer in memory

How can you copy a dictionary or set?

- Use their X.copy() method call (lists have one as of Python 3.3 as well), or - pass the original object to their type names, dict and set 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

Overall picture on variables:

- variables are created when assigned - can reference any type of object -must be assigned before they are referenced

1. Consider the following three statements. Do they change the value printed for A? A = "spam" B = A B = "shrubbery"

1. No: A still prints as "spam". When B is assigned to the string "shrubbery", all that happens is that the variable B is reset to point to the new string object. A and B initially share (i.e., reference/point to) the same single string object "spam", but two names are never linked together in Python. Thus, setting B to a different object has no effect on A. The same would be true if the last statement here were B = B + 'shrubbery', by the way—the concatenation would make a new object for its result, which would then be assigned to B only. We can never overwrite a string (or number, or tuple) in place, because strings are immutable.

2. Consider these three statements. Do they change the printed value of A? A = ["spam"] B = A B[0] = "shrubbery"

2. Yes: A now prints as ["shrubbery"]. Technically, we haven't really changed either A or B; instead, we've changed part of the object they both reference (point to) by overwriting that object in place through the variable B. Because A references the same object as B, the update is reflected in A as well.

3. How about these—is A changed now? A = ["spam"] B = A[:] B[0] = "shrubbery"

3. No: A still prints as ["spam"]. The in-place assignment through B has no effect this time because the slice expression made a copy of the list object before it was assigned to B. After the second assignment statement, there are two different list objects that have the same value (in Python, we say they are ==, but not is). The third statement changes the value of the list object pointed to by B, but not that pointed to by A.

How is a variable created in Python?

A variable (i.e., name), like a, is created when your code first assigns it a value.

What are the variable types in Python?

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.

In Python, how is space reclaimed when a variable is referenced to something else?

Garbage Collection: automatic reclamation of objects

How is a list made in python?

List are made with brackets: >>> L1 = [2, 3, 4] >>> L2 = L1 >>> L1 = [2, 3, 4] # A mutable object >>> L2 = L1 # Make a reference to the same object >>> L1[0] = 24 # An in-place change >>> L1 # L1 is different [24, 3, 4] >>> L2 # But so is L2! [24, 3, 4]

How are lists copied into another variable?

Lists are copied by using slicing: >>> L1 = [2, 3, 4] >>> L2 = L1[:] # Make a copy of L1 (or list(L1), copy.copy(L1), etc.) >>> L1[0] = 24 >>> L1 [24, 3, 4] >>> L2 # L2 is not changed [2, 3, 4] Note: slicing technique won't work on the other major mutable core types, dictionaries and sets, because they are not sequences

What is shared referencing?

Shared referencing is when different variables are referencing the same object

How are variables used in Python?

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.

What is the concrete definiton of a: - variable - objects - references

• 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. • References are automatically followed pointers from variables to objects


संबंधित स्टडी सेट्स

INFUZNÍ ROZTOKY PRO DODÁNÍ ENERGETICKÝCH POTŘEB ORGANISMU - CUKRY

View Set

Chapter 17: Motor Speech Disorders in Adults

View Set

Essentials of Strength Training and Conditioning Ch. 15

View Set

AP World Chapter 4 (1000-30 BCE) Greece and Iran

View Set

Concepts Module 4: Operating Systems and File

View Set

MAN 3025 Test 2 Guide (Ch. 5 ,7, 10, 11, 14)

View Set