Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Sep Dehpour - Magic Method, on the wall, who, n...

Sep Dehpour - Magic Method, on the wall, who, now, is the `__fairest__` one of all?

Magic methods are a very powerful feature of Python and can open a whole new door for you. However, with great power comes great responsibility.

In this talk we explore magic method's capabilities by first designing new interfaces in a series of fun experiments. Secondly, we play with creating undeletable objects and learn about the mighty Garbage Collector in cPython and how a single magic method can overturn the fate of the object. Lastly, we create a lazy Redis client to illustrate a practical application of magic methods and learn about lazy loading.

Once you see what magic methods can bring to the table, the limit is only your imagination!

https://us.pycon.org/2017/schedule/presentation/486/

PyCon 2017

May 21, 2017
Tweet

More Decks by PyCon 2017

Other Decks in Programming

Transcript

  1. Magic Method, on the wall, who, now, is the _

    _ f a i r e s t _ _ one of all? By Sep Dehpour Pycon 2017 zepworks.com sep at zepworks.com https://github.com/seperman
  2. Disclaimer Magic methods are a very powerful feature of Python

    and can open a whole new door for you. However with great power comes great responsibility. The following experiments are solely for educational purposes and NOT for production code.
  3. Experiment: Type Less > > > a = 1 0

    > > > a = a + 2 0 > > > p r i n t ( a ) 3 0
  4. Experiment: Type Less > > > a = 1 0

    > > > a + = 2 0 > > > p r i n t ( a ) 3 0
  5. Experiment: Type Less > > > a = 1 0

    > > > a + 2 0 > > > p r i n t ( a ) 3 0
  6. Experiment 1: Type Less c l a s s N

    u m : d e f _ _ i n i t _ _ ( s e l f , v a l u e ) : s e l f . v a l u e = v a l u e
  7. Experiment 1: Type Less c l a s s N

    u m : d e f _ _ i n i t _ _ ( s e l f , v a l u e ) : s e l f . v a l u e = v a l u e d e f _ _ a d d _ _ ( s e l f , o t h e r ) : s e l f . v a l u e + = o t h e r r e t u r n s e l f . v a l u e
  8. Experiment 1: Type Less c l a s s N

    u m : d e f _ _ i n i t _ _ ( s e l f , v a l u e ) : s e l f . v a l u e = v a l u e d e f _ _ a d d _ _ ( s e l f , o t h e r ) : s e l f . v a l u e + = o t h e r r e t u r n s e l f . v a l u e d e f _ _ s u b _ _ ( s e l f , o t h e r ) : s e l f . v a l u e - = o t h e r r e t u r n s e l f . v a l u e
  9. Experiment 1: Type Less c l a s s N

    u m : d e f _ _ i n i t _ _ ( s e l f , v a l u e ) : s e l f . v a l u e = v a l u e d e f _ _ a d d _ _ ( s e l f , o t h e r ) : s e l f . v a l u e + = o t h e r r e t u r n s e l f . v a l u e d e f _ _ s u b _ _ ( s e l f , o t h e r ) : s e l f . v a l u e - = o t h e r r e t u r n s e l f . v a l u e d e f _ _ r e p r _ _ ( s e l f ) : r e t u r n s t r ( s e l f . v a l u e ) _ _ s t r _ _ = _ _ r e p r _ _
  10. Mutable Integer > > > a = N u m

    ( 1 0 ) > > > a 1 0
  11. Mutable Integer > > > a = N u m

    ( 1 0 ) > > > a 1 0 > > > a + 2 0 > > > a 3 0
  12. Mutable Integer > > > a = N u m

    ( 1 0 ) > > > a 1 0 > > > a + 2 0 > > > a 3 0 > > > a - 5 > > > a 2 5
  13. Mutable Integer > > > a = N u m

    ( 1 0 ) > > > a 1 0 > > > a + 2 0 > > > a 3 0 > > > a - 5 > > > a 2 5 > > > 2 0 + a
  14. Mutable Integer > > > a = N u m

    ( 1 0 ) > > > a 1 0 > > > a + 2 0 > > > a 3 0 > > > a - 5 > > > a 2 5 > > > 2 0 + a T y p e E r r o r : u n s u p p o r t e d o p e r a n d t y p e ( s ) f o r + : ' i n t ' a n d ' N u m '
  15. Experiment: Type Less _ _ a d d _ _

    d e f _ _ a d d _ _ ( s e l f , o t h e r ) : s e l f . v a l u e + = o t h e r r e t u r n s e l f . v a l u e
  16. Experiment: Type Less _ _ a d d _ _

    d e f _ _ a d d _ _ ( s e l f , o t h e r ) : s e l f . v a l u e + = o t h e r r e t u r n s e l f . v a l u e a + 2 0 - - - > a . _ _ a d d _ _ ( a , 2 0 )
  17. Experiment: Type Less _ _ a d d _ _

    d e f _ _ a d d _ _ ( s e l f , o t h e r ) : s e l f . v a l u e + = o t h e r r e t u r n s e l f . v a l u e a + 2 0 - - - > a . _ _ a d d _ _ ( a , 2 0 ) 2 0 + a - - - > i n t . _ _ a d d _ _ ( 2 0 , a )
  18. Experiment: Type Less _ _ a d d _ _

    d e f _ _ a d d _ _ ( s e l f , o t h e r ) : s e l f . v a l u e + = o t h e r r e t u r n s e l f . v a l u e a + 2 0 - - - > a . _ _ a d d _ _ ( a , 2 0 ) 2 0 + a - - - > i n t . _ _ a d d _ _ ( 2 0 , a ) T y p e E r r o r : u n s u p p o r t e d o p e r a n d t y p e ( s ) f o r + : ' i n t ' a n d ' N u m '
  19. Reversed Magic methods They start with r _ _ r

    a d d _ _ and _ _ r s u b _ _
  20. c l a s s N u m : d

    e f _ _ i n i t _ _ ( s e l f , v a l u e ) : s e l f . v a l u e = v a l u e d e f _ _ a d d _ _ ( s e l f , o t h e r ) : s e l f . v a l u e + = o t h e r r e t u r n s e l f . v a l u e d e f _ _ s u b _ _ ( s e l f , o t h e r ) : s e l f . v a l u e - = o t h e r r e t u r n s e l f . v a l u e d e f _ _ r e p r _ _ ( s e l f ) : r e t u r n s t r ( s e l f . v a l u e ) _ _ s t r _ _ = _ _ r e p r _ _
  21. c l a s s N u m : d

    e f _ _ i n i t _ _ ( s e l f , v a l u e ) : s e l f . v a l u e = v a l u e d e f _ _ a d d _ _ ( s e l f , o t h e r ) : s e l f . v a l u e + = o t h e r r e t u r n s e l f . v a l u e d e f _ _ s u b _ _ ( s e l f , o t h e r ) : s e l f . v a l u e - = o t h e r r e t u r n s e l f . v a l u e d e f _ _ r e p r _ _ ( s e l f ) : r e t u r n s t r ( s e l f . v a l u e ) _ _ s t r _ _ = _ _ r e p r _ _ _ _ r a d d _ _ = _ _ a d d _ _
  22. c l a s s N u m : d

    e f _ _ i n i t _ _ ( s e l f , v a l u e ) : s e l f . v a l u e = v a l u e d e f _ _ a d d _ _ ( s e l f , o t h e r ) : s e l f . v a l u e + = o t h e r r e t u r n s e l f . v a l u e d e f _ _ s u b _ _ ( s e l f , o t h e r ) : s e l f . v a l u e - = o t h e r r e t u r n s e l f . v a l u e d e f _ _ r e p r _ _ ( s e l f ) : r e t u r n s t r ( s e l f . v a l u e ) _ _ s t r _ _ = _ _ r e p r _ _ _ _ r a d d _ _ = _ _ a d d _ _ d e f _ _ r s u b _ _ ( s e l f , o t h e r ) : s e l f . v a l u e = o t h e r - s e l f . v a l u e r e t u r n s e l f . v a l u e
  23. Experiment: Type Less > > > a = N u

    m ( 1 0 ) > > > a + 2 0 > > > a 3 0
  24. Experiment: Type Less > > > a = N u

    m ( 1 0 ) > > > a + 2 0 > > > a 3 0 > > > a - 5 > > > a 2 5
  25. Experiment: Type Less > > > a = N u

    m ( 1 0 ) > > > a + 2 0 > > > a 3 0 > > > a - 5 > > > a 2 5 > > > 4 0 + a > > > a 6 5
  26. Experiment: Type Less > > > a = N u

    m ( 1 0 ) > > > a + 2 0 > > > a 3 0 > > > a - 5 > > > a 2 5 > > > 4 0 + a > > > a 6 5 > > > 2 0 - a > > > a - 4 5
  27. Experiment 2: Print Filter f o o = [ 1

    , 2 , 3 , 5 , 6 , 7 ] b a r = f i l t e r ( l a m b d a x : x % 3 = = 0 , f o o )
  28. Experiment 2: Print Filter f o o = [ 1

    , 2 , 3 , 5 , 6 , 7 ] b a r = f i l t e r ( l a m b d a x : x % 3 = = 0 , f o o ) > > > p r i n t ( b a r )
  29. Experiment 2: Print Filter f o o = [ 1

    , 2 , 3 , 5 , 6 , 7 ] b a r = f i l t e r ( l a m b d a x : x % 3 = = 0 , f o o ) > > > p r i n t ( b a r ) < f i l t e r a t 0 x 1 1 9 1 5 1 d 1 8 >
  30. Experiment 2: Print Filter f o o = [ 1

    , 2 , 3 , 5 , 6 , 7 ] b a r = f i l t e r ( l a m b d a x : x % 3 = = 0 , f o o ) > > > p r i n t ( b a r ) [ 3 , 6 ]
  31. Experiment 2: Print Filter f o o = [ 1

    , 2 , 3 , 5 , 6 , 7 ] b a r = f i l t e r ( l a m b d a x : x % 3 = = 0 , f o o ) > > > p r i n t ( l i s t ( b a r ) ) [ 3 , 6 ]
  32. Everything is an object d e f f u n

    c ( x ) : p r e v i o u s _ x = g e t a t t r ( f u n c , " _ x " , " N o t s e t " )
  33. Everything is an object d e f f u n

    c ( x ) : p r e v i o u s _ x = g e t a t t r ( f u n c , " _ x " , " N o t s e t " ) p r i n t ( " n e w v a l u e : { } , p r e v i o u s v a l u e : { } " . f o r m a t ( x , p r e v i o u s _ x ) )
  34. Everything is an object d e f f u n

    c ( x ) : p r e v i o u s _ x = g e t a t t r ( f u n c , " _ x " , " N o t s e t " ) p r i n t ( " n e w v a l u e : { } , p r e v i o u s v a l u e : { } " . f o r m a t ( x , p r e v i o u s _ x ) ) f u n c . _ x = x
  35. Everything is an object d e f f u n

    c ( x ) : p r e v i o u s _ x = g e t a t t r ( f u n c , " _ x " , " N o t s e t " ) p r i n t ( " n e w v a l u e : { } , p r e v i o u s v a l u e : { } " . f o r m a t ( x , p r e v i o u s _ x ) ) f u n c . _ x = x > > > f u n c ( 1 0 ) n e w v a l u e : 1 0 , p r e v i o u s v a l u e : N o t s e t
  36. Everything is an object d e f f u n

    c ( x ) : p r e v i o u s _ x = g e t a t t r ( f u n c , " _ x " , " N o t s e t " ) p r i n t ( " n e w v a l u e : { } , p r e v i o u s v a l u e : { } " . f o r m a t ( x , p r e v i o u s _ x ) ) f u n c . _ x = x > > > f u n c ( 1 0 ) n e w v a l u e : 1 0 , p r e v i o u s v a l u e : N o t s e t > > > f u n c ( 2 0 ) n e w v a l u e : 2 0 , p r e v i o u s v a l u e : 1 0
  37. Everything is an object d e f f u n

    c ( x ) : p r e v i o u s _ x = g e t a t t r ( f u n c , " _ x " , " N o t s e t " ) p r i n t ( " n e w v a l u e : { } , p r e v i o u s v a l u e : { } " . f o r m a t ( x , p r e v i o u s _ x ) ) f u n c . _ x = x > > > f u n c ( 1 0 ) n e w v a l u e : 1 0 , p r e v i o u s v a l u e : N o t s e t > > > f u n c ( 2 0 ) n e w v a l u e : 2 0 , p r e v i o u s v a l u e : 1 0 > > > f u n c ( 3 0 ) n e w v a l u e : 3 0 , p r e v i o u s v a l u e : 2 0
  38. Experiment 2: Print Filter c l a s s F

    i l t e r ( f i l t e r ) :
  39. Experiment 2: Print Filter c l a s s F

    i l t e r ( f i l t e r ) : d e f _ _ s t r _ _ ( s e l f ) : r e t u r n s t r ( l i s t ( s e l f ) ) _ _ r e p r _ _ = _ _ s t r _ _
  40. Experiment 2: Print Filter c l a s s F

    i l t e r ( f i l t e r ) : d e f _ _ s t r _ _ ( s e l f ) : r e t u r n s t r ( l i s t ( s e l f ) ) _ _ r e p r _ _ = _ _ s t r _ _ b a r = F i l t e r ( l a m b d a x : x % 3 = = 0 , f o o )
  41. Experiment 2: Print Filter c l a s s F

    i l t e r ( f i l t e r ) : d e f _ _ s t r _ _ ( s e l f ) : r e t u r n s t r ( l i s t ( s e l f ) ) _ _ r e p r _ _ = _ _ s t r _ _ b a r = F i l t e r ( l a m b d a x : x % 3 = = 0 , f o o ) > > > p r i n t ( b a r ) [ 3 , 6 ]
  42. Experiment 3: Echo to file e c h o "

    h e l l o " > > f o o . t x t
  43. Experiment 3: Echo to file Python 2 only p r

    i n t > > m y f i l e , " H e l l o W o r l d ! \ n " p r i n t > > m y f i l e , " I w a n t a b u r r i t o . "
  44. Experiment 3: Echo to file m y f i l

    e = o p e n ( " h e l l o . t x t " , " w " )
  45. Experiment 3: Echo to file m y f i l

    e = o p e n ( " h e l l o . t x t " , " w " ) c l a s s E c h o : d e f _ _ i n i t _ _ ( s e l f , t e x t ) : s e l f . t e x t = t e x t
  46. Experiment 3: Echo to file m y f i l

    e = o p e n ( " h e l l o . t x t " , " w " ) c l a s s E c h o : d e f _ _ i n i t _ _ ( s e l f , t e x t ) : s e l f . t e x t = t e x t d e f _ _ r s h i f t _ _ ( s e l f , o t h e r ) : o t h e r . s e e k ( 0 , 2 ) # s e e k e n d o f t h e f i l e o t h e r . w r i t e ( s e l f . t e x t )
  47. Experiment 3: Echo to file m y f i l

    e = o p e n ( " h e l l o . t x t " , " w " ) c l a s s E c h o : d e f _ _ i n i t _ _ ( s e l f , t e x t ) : s e l f . t e x t = t e x t d e f _ _ r s h i f t _ _ ( s e l f , o t h e r ) : o t h e r . s e e k ( 0 , 2 ) o t h e r . w r i t e ( s e l f . t e x t ) # W r i t e s t o t h e e n d o f t h e f i l e ! E c h o ( " H e l l o W o r l d ! \ n " ) > > m y f i l e E c h o ( " W a n t a b u r r i t o ? " ) > > m y f i l e
  48. Experiment 4: Pipe and Grep c o m m a

    n d | g r e p s o m e t h i n g
  49. Experiment 4: Pipe and Grep c l a s s

    G r e p : d e f _ _ i n i t _ _ ( s e l f , i t e m ) : s e l f . i t e m = i t e m . l o w e r ( ) d e f _ _ o r _ _ ( s e l f , o t h e r ) : . . .
  50. Experiment 4: Pipe and Grep c l a s s

    G r e p : d e f _ _ i n i t _ _ ( s e l f , i t e m ) : s e l f . i t e m = i t e m . l o w e r ( ) d e f _ _ o r _ _ ( s e l f , o t h e r ) : . . . g r e p ( s o m e t h i n g ) | t e x t
  51. Experiment 4: Pipe and Grep c l a s s

    G r e p : d e f _ _ i n i t _ _ ( s e l f , i t e m ) : s e l f . i t e m = i t e m . l o w e r ( ) d e f _ _ o r _ _ ( s e l f , o t h e r ) : . . . g r e p ( s o m e t h i n g ) | t e x t t e x t | g r e p ( s o m e t h i n g )
  52. Experiment 4: Pipe and Grep c l a s s

    G r e p : d e f _ _ i n i t _ _ ( s e l f , i t e m ) : s e l f . i t e m = i t e m . l o w e r ( )
  53. Experiment 4: Pipe and Grep c l a s s

    G r e p : d e f _ _ i n i t _ _ ( s e l f , i t e m ) : s e l f . i t e m = i t e m . l o w e r ( ) d e f i s _ i t e m _ i n _ l i n e ( s e l f , l i n e ) : r e t u r n s e l f . i t e m i n l i n e
  54. Experiment 4: Pipe and Grep c l a s s

    G r e p : d e f _ _ i n i t _ _ ( s e l f , i t e m ) : s e l f . i t e m = i t e m . l o w e r ( ) d e f i s _ i t e m _ i n _ l i n e ( s e l f , l i n e ) : r e t u r n s e l f . i t e m i n l i n e d e f _ _ r o r _ _ ( s e l f , o t h e r ) : i f i s i n s t a n c e ( o t h e r , s t r ) : o t h e r = o t h e r . l o w e r ( ) . s p l i t ( ' \ n ' )
  55. Experiment 4: Pipe and Grep c l a s s

    G r e p : d e f _ _ i n i t _ _ ( s e l f , i t e m ) : s e l f . i t e m = i t e m . l o w e r ( ) d e f i s _ i t e m _ i n _ l i n e ( s e l f , l i n e ) : r e t u r n s e l f . i t e m i n l i n e d e f _ _ r o r _ _ ( s e l f , o t h e r ) : i f i s i n s t a n c e ( o t h e r , s t r ) : o t h e r = o t h e r . l o w e r ( ) . s p l i t ( ' \ n ' ) r e t u r n F i l t e r ( s e l f . i s _ i t e m _ i n _ l i n e , o t h e r )
  56. Experiment 4: Pipe and Grep > > > l i

    n e s = " " " W h e t h e r y o u ' r e n e w t o p r o g r a m m i n g o r a n e x p e r i e n c e d d e v e l o p e r , i t ' s e a s y t o l e a r n a n d u s e P y t h o n . C h e c k o u t j o b s . p y t h o n . o r g f o r P y t h o n j o b s . " " "
  57. Experiment 4: Pipe and Grep > > > l i

    n e s = " " " W h e t h e r y o u ' r e n e w t o p r o g r a m m i n g o r a n e x p e r i e n c e d d e v e l o p e r , i t ' s e a s y t o l e a r n a n d u s e P y t h o n . C h e c k o u t j o b s . p y t h o n . o r g f o r P y t h o n j o b s . " " " > > > l i n e s | G r e p ( ' P y t h o n ' ) [ ' t o l e a r n a n d u s e P y t h o n . ' , ' c h e c k o u t j o b s . p y t h o n . o r g ' , ' f o r p y t h o n j o b s . ' ]
  58. Experiment 4: Pipe and Grep > > > l i

    n e s = " " " W h e t h e r y o u ' r e n e w t o p r o g r a m m i n g o r a n e x p e r i e n c e d d e v e l o p e r , i t ' s e a s y t o l e a r n a n d u s e P y t h o n . C h e c k o u t j o b s . p y t h o n . o r g f o r P y t h o n j o b s . " " " > > > l i n e s | G r e p ( ' P y t h o n ' ) | G r e p ( ' j o b s ' ) [ ' c h e c k o u t j o b s . p y t h o n . o r g ' , ' f o r p y t h o n j o b s . ' ]
  59. Experiment 5: The undeletable > > > d e l

    n o r m a l _ o b j e c t
  60. Experiment 5: The undeletable > > > d e l

    n o r m a l _ o b j e c t > > > n o r m a l _ o b j e c t
  61. Experiment 5: The undeletable > > > d e l

    n o r m a l _ o b j e c t > > > n o r m a l _ o b j e c t T r a c e b a c k ( m o s t r e c e n t c a l l l a s t ) : F i l e " < s t d i n > " , l i n e 1 , i n < m o d u l e > N a m e E r r o r : n a m e ' n o r m a l _ o b j e c t ' i s n o t d e f i n e d
  62. Experiment 5: The undeletable > > > d e l

    o b j < o b j : I ' m s t i l l h e r e . Y o u C A N N O T d e l e t e m e ! >
  63. Experiment 5: The undeletable > > > d e l

    o b j < o b j : I ' m s t i l l h e r e . Y o u C A N N O T d e l e t e m e ! > > > > o b j < Y e s I ' m s t i l l h e r e ! >
  64. Reference counting i m p o r t g c

    c l a s s A : d e f _ _ s t r _ _ ( s e l f ) : r e t u r n " A o b j " _ _ r e p r _ _ = _ _ s t r _ _
  65. Reference counting i m p o r t g c

    c l a s s A : d e f _ _ s t r _ _ ( s e l f ) : r e t u r n " A o b j " _ _ r e p r _ _ = _ _ s t r _ _ a = A ( ) p r i n t ( l e n ( g c . g e t _ r e f e r r e r s ( a ) ) ) # p r i n t s 1
  66. Reference counting i m p o r t g c

    c l a s s A : d e f _ _ s t r _ _ ( s e l f ) : r e t u r n " A o b j " _ _ r e p r _ _ = _ _ s t r _ _ a = A ( ) p r i n t ( l e n ( g c . g e t _ r e f e r r e r s ( a ) ) ) # p r i n t s 1 d e f p r i n t _ r e f _ c o u n t ( o b j ) : p r i n t ( l e n ( g c . g e t _ r e f e r r e r s ( o b j ) ) )
  67. Reference counting i m p o r t g c

    c l a s s A : d e f _ _ s t r _ _ ( s e l f ) : r e t u r n " A o b j " _ _ r e p r _ _ = _ _ s t r _ _ a = A ( ) p r i n t ( l e n ( g c . g e t _ r e f e r r e r s ( a ) ) ) # p r i n t s 1 d e f p r i n t _ r e f _ c o u n t ( o b j ) : p r i n t ( l e n ( g c . g e t _ r e f e r r e r s ( o b j ) ) ) p r i n t _ r e f _ c o u n t ( a )
  68. Reference counting i m p o r t g c

    c l a s s A : d e f _ _ s t r _ _ ( s e l f ) : r e t u r n " A o b j " _ _ r e p r _ _ = _ _ s t r _ _ a = A ( ) p r i n t ( l e n ( g c . g e t _ r e f e r r e r s ( a ) ) ) # p r i n t s 1 d e f p r i n t _ r e f _ c o u n t ( o b j ) : p r i n t ( l e n ( g c . g e t _ r e f e r r e r s ( o b j ) ) ) p r i n t _ r e f _ c o u n t ( a ) # p r i n t s 2
  69. Reference counting i m p o r t g c

    c l a s s A : d e f _ _ s t r _ _ ( s e l f ) : r e t u r n " A o b j " _ _ r e p r _ _ = _ _ s t r _ _ a = A ( ) p r i n t ( l e n ( g c . g e t _ r e f e r r e r s ( a ) ) ) # p r i n t s 1 d e f p r i n t _ r e f _ c o u n t ( o b j ) : p r i n t ( l e n ( g c . g e t _ r e f e r r e r s ( o b j ) ) ) p r i n t _ r e f _ c o u n t ( a ) # p r i n t s 2 p r i n t ( l e n ( g c . g e t _ r e f e r r e r s ( a ) ) ) # p r i n t s 1
  70. Reference counting c = { 2 : a } d

    = { 1 : a } > > > p r i n t _ r e f _ c o u n t ( a ) 4
  71. Reference counting c = { 2 : a } d

    = { 1 : a } > > > p r i n t _ r e f _ c o u n t ( a ) 4 > > > p r i n t ( a ) A o b j > > > p r i n t ( d [ 1 ] ) A o b j
  72. Reference counting c = { 2 : a } d

    = { 1 : a } > > > p r i n t _ r e f _ c o u n t ( a ) 4 > > > p r i n t ( a ) A o b j > > > p r i n t ( d [ 1 ] ) A o b j > > > d e l a
  73. Reference counting c = { 2 : a } d

    = { 1 : a } > > > p r i n t _ r e f _ c o u n t ( a ) 4 > > > p r i n t ( a ) A o b j > > > p r i n t ( d [ 1 ] ) A o b j > > > d e l a > > > p r i n t ( d [ 1 ] )
  74. Reference counting c = { 2 : a } d

    = { 1 : a } > > > p r i n t _ r e f _ c o u n t ( a ) 4 > > > p r i n t ( a ) A o b j > > > p r i n t ( d [ 1 ] ) A o b j > > > d e l a > > > p r i n t ( d [ 1 ] ) A o b j
  75. Reference counting c = { 2 : a } d

    = { 1 : a } > > > p r i n t _ r e f _ c o u n t ( a ) 4 > > > p r i n t ( a ) A o b j > > > p r i n t ( d [ 1 ] ) A o b j > > > d e l a > > > p r i n t ( d [ 1 ] ) A o b j > > > p r i n t _ r e f _ c o u n t ( d [ 1 ] )
  76. Reference counting c = { 2 : a } d

    = { 1 : a } > > > p r i n t _ r e f _ c o u n t ( a ) 4 > > > p r i n t ( a ) A o b j > > > p r i n t ( d [ 1 ] ) A o b j > > > d e l a > > > p r i n t ( d [ 1 ] ) A o b j > > > p r i n t _ r e f _ c o u n t ( d [ 1 ] ) 3
  77. before c l a s s A : d e

    f _ _ s t r _ _ ( s e l f ) : r e t u r n " A o b j " _ _ r e p r _ _ = _ _ s t r _ _
  78. after c l a s s A : d e

    f _ _ s t r _ _ ( s e l f ) : r e t u r n " A o b j " _ _ r e p r _ _ = _ _ s t r _ _ d e f _ _ d e l _ _ ( s e l f ) : p r i n t ( " D e l e t i n g a " )
  79. c = { 2 : a } d = {

    1 : a } > > > p r i n t _ r e f _ c o u n t ( a ) 4
  80. c = { 2 : a } d = {

    1 : a } > > > p r i n t _ r e f _ c o u n t ( a ) 4 > > > d e l a
  81. c = { 2 : a } d = {

    1 : a } > > > p r i n t _ r e f _ c o u n t ( a ) 4 > > > d e l a > > > p r i n t ( d [ 1 ] ) A o b j > > > p r i n t _ r e f _ c o u n t ( d [ 1 ] ) 3
  82. c = { 2 : a } d = {

    1 : a } > > > p r i n t _ r e f _ c o u n t ( a ) 4 > > > d e l a > > > p r i n t ( d [ 1 ] ) A o b j > > > p r i n t _ r e f _ c o u n t ( d [ 1 ] ) 3 > > > d e l c > > > p r i n t _ r e f _ c o u n t ( d [ 1 ] ) 2
  83. c = { 2 : a } d = {

    1 : a } > > > p r i n t _ r e f _ c o u n t ( a ) 4 > > > d e l a > > > p r i n t ( d [ 1 ] ) A o b j > > > p r i n t _ r e f _ c o u n t ( d [ 1 ] ) 3 > > > d e l c > > > p r i n t _ r e f _ c o u n t ( d [ 1 ] ) 2 > > > e x i t ( )
  84. c = { 2 : a } d = {

    1 : a } > > > p r i n t _ r e f _ c o u n t ( a ) 4 > > > d e l a > > > p r i n t ( d [ 1 ] ) A o b j > > > p r i n t _ r e f _ c o u n t ( d [ 1 ] ) 3 > > > d e l c > > > p r i n t _ r e f _ c o u n t ( d [ 1 ] ) 2 > > > e x i t ( ) D e l e t i n g a
  85. c = { 2 : a } d = {

    1 : a } > > > p r i n t _ r e f _ c o u n t ( a ) 4 > > > d e l a > > > p r i n t ( d [ 1 ] ) A o b j > > > p r i n t _ r e f _ c o u n t ( d [ 1 ] ) 3 > > > d e l c > > > p r i n t _ r e f _ c o u n t ( d [ 1 ] ) 2 > > > d e l d
  86. c = { 2 : a } d = {

    1 : a } > > > p r i n t _ r e f _ c o u n t ( a ) 4 > > > d e l a > > > p r i n t ( d [ 1 ] ) A o b j > > > p r i n t _ r e f _ c o u n t ( d [ 1 ] ) 3 > > > d e l c > > > p r i n t _ r e f _ c o u n t ( d [ 1 ] ) 2 > > > d e l d D e l e t i n g a > > > e x i t ( )
  87. Experiment 5: The undeletable _ _ d e l _

    _ > > > d e l o b j < o b j : I ' m s t i l l h e r e . Y o u C A N N O T d e l e t e m e ! > > > > o b j < Y e s I ' m s t i l l h e r e ! >
  88. The undeletable: idea 1: stop _ _ d e l

    _ _ c l a s s O b j : d e f _ _ d e l _ _ ( s e l f ) : r a i s e E x c e p t i o n ( ' Y o u c a n n o t d e l e t e m e ' )
  89. The undeletable: Idea 2: Resurrect c l a s s

    O b j : d e f _ _ d e l _ _ ( s e l f ) :
  90. The undeletable: Idea 2: Resurrect c l a s s

    O b j : d e f _ _ d e l _ _ ( s e l f ) : g l o b a l o b j
  91. The undeletable: Idea 2: Resurrect c l a s s

    O b j : d e f _ _ d e l _ _ ( s e l f ) : g l o b a l o b j o b j = s e l f p r i n t ( " Y o u c a n ' t d e l e t e m e ! " ) d e f _ _ s t r _ _ ( s e l f ) : r e t u r n " < o b j : { } > " . f o r m a t ( i d ( s e l f ) )
  92. The undeletable: Idea 2: Resurrect c l a s s

    O b j : d e f _ _ d e l _ _ ( s e l f ) : g l o b a l o b j o b j = s e l f p r i n t ( " Y o u c a n ' t d e l e t e m e ! " ) d e f _ _ s t r _ _ ( s e l f ) : r e t u r n " < o b j : { } > " . f o r m a t ( i d ( s e l f ) ) > > > o b j = O b j ( ) > > > p r i n t ( o b j ) < o b j : 1 2 3 1 2 3 > > > > d e l o b j
  93. The undeletable: Idea 2: Resurrect c l a s s

    O b j : d e f _ _ d e l _ _ ( s e l f ) : g l o b a l o b j o b j = s e l f p r i n t ( " Y o u c a n ' t d e l e t e m e ! " ) d e f _ _ s t r _ _ ( s e l f ) : r e t u r n " < o b j : { } > " . f o r m a t ( i d ( s e l f ) ) > > > o b j = O b j ( ) > > > p r i n t ( o b j ) < o b j : 1 2 3 1 2 3 > > > > d e l o b j Y o u c a n ' t d e l e t e m e !
  94. The undeletable: Idea 2: Resurrect c l a s s

    O b j : d e f _ _ d e l _ _ ( s e l f ) : g l o b a l o b j o b j = s e l f p r i n t ( " Y o u c a n ' t d e l e t e m e ! " ) d e f _ _ s t r _ _ ( s e l f ) : r e t u r n " < o b j : { } > " . f o r m a t ( i d ( s e l f ) ) > > > o b j = O b j ( ) > > > p r i n t ( o b j ) < o b j : 1 2 3 1 2 3 > > > > d e l o b j Y o u c a n ' t d e l e t e m e ! > > > p r i n t ( o b j )
  95. The undeletable: Idea 2: Resurrect c l a s s

    O b j : d e f _ _ d e l _ _ ( s e l f ) : g l o b a l o b j o b j = s e l f p r i n t ( " Y o u c a n ' t d e l e t e m e ! " ) d e f _ _ s t r _ _ ( s e l f ) : r e t u r n " < o b j : { } > " . f o r m a t ( i d ( s e l f ) ) > > > o b j = O b j ( ) > > > p r i n t ( o b j ) < o b j : 1 2 3 1 2 3 > > > > d e l o b j Y o u c a n ' t d e l e t e m e ! > > > p r i n t ( o b j ) < o b j : 1 2 3 1 2 3 >
  96. p r i n t ( o b j )

    d e l o b j p r i n t ( o b j )
  97. Python 3.4+ PEP 442 < o b j : 4

    4 8 2 7 8 6 1 9 2 > Y o u c a n ' t d e l e t e m e ! < o b j : 4 4 8 2 7 8 6 1 9 2 >
  98. Python 2 to 3.3 < o b j : 4

    5 4 9 4 3 5 7 6 0 > Y o u c a n ' t d e l e t e m e ! < o b j : 4 5 4 9 4 3 5 7 6 0 > Y o u c a n ' t d e l e t e m e ! Y o u c a n ' t d e l e t e m e !
  99. What we learnt Making a number object so we type

    less Subclassed filter so we print the filtered and see the results Echo to file: E c h o ( " H e l l o W o r l d ! \ n " ) > > m y f i l e Pipe and grep l i n e s | G r e p ( ' P y t h o n ' ) | G r e p ( ' j o b s ' ) Undeletable object > > > d e l o b j Y o u c a n ' t d e l e t e m e !
  100. bad‑ideas p i p i n s t a l

    l b a d - i d e a s It is a good idea to install bad ideas!
  101. bad‑ideas But use them at your own risk. > >

    > f r o m b a d i m p o r t g r e p > > > L I N E S | g r e p ( ' P y t h o n ' ) | g r e p ( ' j o b s ' ) [ ' c h e c k o u t j o b s . p y t h o n . o r g ' , ' f o r p y t h o n j o b s . ' ] . . . > > > f r o m b a d i m p o r t U n d e l e t a b l e > > > o b j = U n d e l e t a b l e ( ) > > > d e l o b j Y o u c a n ' t d e l e t e m e ! . . .
  102. Magic Method, on the wall, who, now, is the _

    _ f a i r e s t _ _ one of all? Article: http://zepworks.com/blog/magic‑method‑on‑the‑wall Code: https://github.com/seperman/bad‑ideas ‑‑ Sep Dehpour Pycon 2017 zepworks.com hello at zepworks.com https://github.com/seperman