данных иммутабельные типы - SkyDNS...compile-time constant Objects...

Preview:

Citation preview

Мутабельные и иммутабельные типы данныхИван Феофанов

apiqa.io

02

5

03

2

04

1

05

Immutable Mutable

int, float, complex list, set

tuple, frozenset dict

bytes bytearray

str

bool

Python data types

Immutable

a = 5

a 5

a += 2

a 7

id = 11197536

id = 11197568

b = a

b

06

WAT?

07

Mutable

a = [5, 10, 15]

a [5, 10, 15]

b = a

bid = 139872397262408

a += [20]

[5, 10, 15, 20]

08

Pitfalls

09

● default mutable arguments● side-effects

Default mutable argument

10

Default mutable argument: solution

11

PEP 505 -- None-aware operators (??=)

12

Side-effect

13

compile-time constant

Objects optimization

14

Mutable?

Create new object

Yes

Object type?

value in [-5...256]?

result of constant folding

int

stringNo

No

Refer to existing object

Yes

Yes

object length > 20?No

Yes

No

Yes

contains only ASCII letters, digits or underscores

No No

Yes

Contacts and links

@feofanov github.com/Ivan-Feofanov

bonus

+=

Recommended