Object Internals in Python - Mridu Bhatnagar Who am I? - Python - - PowerPoint PPT Presentation

object internals in python
SMART_READER_LITE
LIVE PREVIEW

Object Internals in Python - Mridu Bhatnagar Who am I? - Python - - PowerPoint PPT Presentation

Object Internals in Python - Mridu Bhatnagar Who am I? - Python Enthusiast - Backend Developer by profession. - I love speaking at various meetup groups and conferences. - Twitter handle - @Mridu__ - objects - memory address Learning -


slide-1
SLIDE 1

Object Internals in Python

  • Mridu Bhatnagar
slide-2
SLIDE 2

Who am I?

  • Python Enthusiast
  • Backend Developer by profession.
  • I love speaking at various meetup groups and

conferences.

  • Twitter handle - @Mridu__
slide-3
SLIDE 3

Learning Objectives

  • objects
  • memory address
  • type of objects (mutable

and immutable objects)

  • difference between is vs ==
  • perator
  • optimizations
slide-4
SLIDE 4

An object is an entity that has attribute and methods associated with it.

slide-5
SLIDE 5

Example:

slide-6
SLIDE 6
slide-7
SLIDE 7

The location where the object gets stored in memory is referred to as memory address.

slide-8
SLIDE 8

Pictorial Representation

slide-9
SLIDE 9

id(object) - id is a built-in function. It is used to determine the memory address of the object.

slide-10
SLIDE 10
slide-11
SLIDE 11

Mutable objects and Immutable objects.

slide-12
SLIDE 12

Objects of built-in type (list, dictionary, sets) are mutable.

slide-13
SLIDE 13
slide-14
SLIDE 14

Objects of built-in type (int, float, bool, str, tuple) are immutable.

slide-15
SLIDE 15
slide-16
SLIDE 16

Difference between is vs == operator

slide-17
SLIDE 17
slide-18
SLIDE 18
slide-19
SLIDE 19

Different use cases to discuss memory

  • ptimization in Python
slide-20
SLIDE 20
  • 1. Sort and Sorted built-in methods
slide-21
SLIDE 21
slide-22
SLIDE 22
  • 2. Concept of Integer Caching
slide-23
SLIDE 23
slide-24
SLIDE 24
  • 3. Concept of String Interning
slide-25
SLIDE 25

As the Python code compiles identifiers are interned.

  • variable names
  • function names
  • class names

Rule: * start with _ or a letter. * may contain _, letter, numbers.

slide-26
SLIDE 26
slide-27
SLIDE 27
slide-28
SLIDE 28
  • 4. Copying List using Assignment
  • perator
slide-29
SLIDE 29
slide-30
SLIDE 30
  • 5. Shallow Copy in Lists
slide-31
SLIDE 31

The outermost container is duplicated, but the copy is filled with references to the same items held by the original container.

slide-32
SLIDE 32
slide-33
SLIDE 33
  • 6. Deep Copy in Lists
slide-34
SLIDE 34

Duplicates do not share references of embedded objects.

slide-35
SLIDE 35
slide-36
SLIDE 36

Why is it important to learn object internals?

slide-37
SLIDE 37

* operator copies the memory references

slide-38
SLIDE 38
slide-39
SLIDE 39

Summary

  • object internals
  • memory address use cases
  • type of objects (mutable,

immutable)

  • difference between is vs ==
  • perator
  • optimizations
slide-40
SLIDE 40