# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

a = 1000
b = 1000

a == b

a is b

c = (1, 2, [])

c[-1].append(1)

c

c[0] = 100

c[-1] = 'a'

# ## tuple

d = (4, 5)
c + d

t1 = (1, 2, [3, 4])
t2 = (1, 2, [3, 4])

t1 == t2

id(t1)

id(t2)

t1[-1].append(5)

t1

t1 == t2

t1[-1] += [6]

l = [1]

id(l)

l.append(2)

id(l)

l += [3]

id(l)

l

# ## 浅复制

l1 = [3, [4, 5], [6, 7]]
l2 = list(l1)

id(l1[0]) == id(l2[0])

l1[0] = 10
id(l1[0]) == id(l2[0])

id(l1[1]) == id(l2[1])

id(l1[2]) == id(l2[2])

id(l1) == id(l2)

l1[0] += (4,)

id(l1[0]), id(l2[0])

l1.remove([4,5])

l1, l2

id(l1)

id(l2)


# ## 深复制

class Bus:
    
    def __init__(self, passengers=None):
        if passengers is None:
            self.passengers = []
        else:
            self.passengers = list(passengers)
            
    def pick(self, name):
        self.passengers.append(name)
        
    def drop(self, name):
        self.passengers.remove(name)


# +
import copy

bus1 = Bus(['Alice', 'Bill', 'Claire', 'David'])
bus2 = copy.copy(bus1)
bus3 = copy.deepcopy(bus1)
# -

id(bus1), id(bus2), id(bus3)

bus1.drop('Bill')

bus2.passengers

id(bus1.passengers), id(bus2.passengers), id(bus3.passengers)

bus3.passengers

# ### deepcopy函数会记住已经复制的对象，可以优雅的处理循环引用

a = [10, 20]
b = [a, 30]
a.append(b)

a

a[-1]

a[0]

a[1]

a[2]

from copy import deepcopy
c = deepcopy(a)
c


# ## 不要使用可变类型作为可选参数的默认值

class HauntedBus:
    
    def __init__(self, passengers=[]):
        self.passengers = passengers
            
    def pick(self, name):
        self.passengers.append(name)
        
    def drop(self, name):
        self.passengers.remove(name)


bus1 = HauntedBus(['Alice', 'Bill'])
print("bus1's passengers: ", bus1.passengers)

bus2 = HauntedBus()
print(bus2.passengers)

bus2.pick('Tom')
print(bus2.passengers)

bus3 = HauntedBus()
print(bus3.passengers)

dir(HauntedBus.__init__)

HauntedBus.__init__.__defaults__[0]

HauntedBus.__init__.__defaults__[0] is bus2.passengers

a = [1,2,3]
c = a
b = list(a)

id(a), id(b), id(c)

# ## 对象的销毁

import weakref

s1 = {1, 2, 3}
s2 = s1


def bye():
    print('Gone with the wind...')


ender = weakref.finalize(s1, bye)

ender.alive

del s1

ender.alive

s2 = 'spam'

ender.alive

# ## 弱引用

a_set = {0, 1}
wref = weakref.ref(a_set)

wref

wref()

a_set = {5, 6}

wref()

wref() is None

wref() is None

wref() is None



class Cheese:
    
    def __init__(self, kind):
        self.kind = kind
        
    def __repr__(self):
        return 'Cheese(%r)' % self.kind


stock = weakref.WeakValueDictionary()
catalog = [Cheese('Red Leicester'), Cheese('Tilsit'), Cheese('Brie'), Cheese('Parmesan')] 

for cheese in catalog:
    stock[cheese.kind] = cheese

sorted(stock.keys())

del catalog

sorted(stock.keys())

cheese

del cheese

sorted(stock.keys())

sorted(stock.keys())

t = (1, 2, 3)
t2 = tuple(t)

id(t) == id(t2)

l = [1, 2, 3]
l2 = list(l)

id(l) == id(l2)



StarcoderdataJupyterScriptsDedupFiltered