Section 09: Object Identity, Lists and Shallows, and Deep copies Flashcards
String Methods:
Strip method:
s.strip.(chars)
Strip Method
s.strip(chars)
: returns copy string with leading and trailing characters in chars removed
- if char not provided → whitespace default
For example, if we have a string s = " hello world "
, s.strip()
would return "hello world"
. If we pass additional arguments to the strip
method, it will remove those characters instead of whitespace. For example, s.strip('dlo ')
would return "hello wor"
.
s not modified ⇒ string immutable
```jsx
»> s = ‘ apple ‘
»> s.strip()
‘apple’
~~~
String Methods
Split Method
s.split(sep)
Split Method
s.split(sep)
: returns list of substrings in string using sep as delimiter
- If sep not provided → whitespace default
```jsx
»> s = ‘bananas and appels’
»> s.split()
[‘bananas’, ‘and’, ‘apples’]
»> s
‘bananas and apples’
»> s.split(‘a’)
[‘b’, ‘n’, ‘n’, ‘s’, ‘nd’, ‘pples’]
~~~
String Methods
Join Method
s.join(x)
> s.join(x)
takes input and iterable object x
(eg. list) containing strings.
Returns new string with string s
inserted between each string in x
```jsx
»> ‘.’.join([‘cat’, ‘dog’])
‘cot.dog’
»> ‘ ‘.join([‘cat’, ‘and’, ‘dog’])
‘cat and dog’
»> ‘ ‘.join(‘apple’)
‘a p p l e’
~~~
What are the two ways to add items to a list?
```jsx
»> x = []
»> x = x + [‘first’]
OR
»> x = []
»> x.append(‘first’)
~~~
What is the ‘identification’ an object gets at its creation?
- Each object assigned identifier at its creation (memory address)
- Identity unique and constant for object during lifetime
Built in function id()
function returns identity of object
```jsx
»> weather_str = ‘It's raining’
»> print(id(weather_str))
446656232
~~~
How can running your program in a module and shell change the output for booleans?
Python uses caching for all immutable objects of built-in types. Therefore, (sometimes) if you use instructions in a shell you’ll see False dispalyed, but if you run it in the module, all print statements will display True
Reference and Value
For copying references?
<aside>
💡 Python uses caching for all immutable objects of built-in types. Therefore, (sometimes) if you use instructions in a *shell* you’ll see `False` dispalyed, but if you run it in the module, all print statements will display `True`
</aside>
For: a = 14
and a = b
, copying reference (assigning memory address) of object a
in b
- Verify uding the
id()
function which returns reference value
```python
»> a = 14
»> b = a
»> id(a)
44480656
»> id(b)
44480656
~~~
Two different IDs point to two different objects
```python
»> x = 500
»> y = 400
»> id(x) == id(y)
False
> > > x = 500
y = 500
id(x) == id(y)
False #Though they have the same value, different IDs
~~~
How can two different IDs point to two different object even if their value assignment is the same?
Two different IDs point to two different objects
```python
»> x = 500
»> y = 400
»> id(x) == id(y)
False
> > > x = 500
y = 500
id(x) == id(y)
False #Though they have the same value, different IDs
~~~
What are the identity operators?
is
and is not
are boolean operators used to determine if the given operands have the same identity (refers to the same object)
Not the same as checking for equality (==
) → refers to havinng the same data, but it does not mean that it is the same object
What is the difference between a deep and shallow copy?
A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original. A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.
Shallow vs Deep
What is a shallow copy?
- When calling shadow copy, modifying one list will modify the other and visa versa
- Reference the same list
- Same reference ID
```python
»> list1 = [‘1’, ‘2’]
»> list2 = list1
»> list2[1] = ‘B’
»> print(list2)
[‘1’,’2’,’B’]
»> print(list1)
[‘1’,’2’,’B’]
~~~
What are list arguments?
Read section 10.12 (p.97)
When pass a list to a function, function get reference to a list.
- If function modifies the list, caller sees change
```python
def delete_head(t):
del t[0]
> > > letters = [‘a’, ‘b’, ‘c’]
delete_head(letters)
letters [‘b’, ‘c’]
~~~
Mutable vs. Immutable
Can be written as void
> Immutable: content object cannot be changed after object’s creation
Strings: immutable
- Cant use the square brackets to modify character in the string
```python
»> s = ‘cats’
»> s[0] = ‘r’
**TypeError**
~~~
> Mutable: content of object can be changed
Lists: mutable
- Can use square brackets to change
Example: function takes list of integers as input and increases the value of each element in list by its index:
```python
def increase_by_index(input_list):
for i in range(len(input_list)):
input_list[i] = input_list[i] + 1
return input_list
~~~
What modifies a list?
Which of the operators/methods we have seen modify the content of list object?
- Assigning value to an element using its index
- Using slice assignment to modify the elements of a list
- Using methods list
append(), insert(), remove(), pop(), or sort()
What does not modify a list?
- Slicing!(It is useful tool to create new lists out of existing lists)
- The
+
and*
operators