Slide 1

Slide 1 text

_ @chewxy #SyPy August 2017 Follow @chewxy on Twi

Slide 2

Slide 2 text

_ def foo(): return 1, 2.0, "hello", [3,4,5], (6,7) _, x, _, y_, _, _ = foo() # x = 2.0 # y = [3,4,5] Follow @chewxy on Twi

Slide 3

Slide 3 text

_ Follow @chewxy on Twi

Slide 4

Slide 4 text

_name _x = "internal use only" Follow @chewxy on Twi

Slide 5

Slide 5 text

_name class Foo: def __init__(self, x): self._x = x >>> f = Foo(1) >>> f._x # still accessible! 1 Follow @chewxy on Twi

Slide 6

Slide 6 text

_name A Gotcha: In mypkg.py: _x = "internal use only" x = "externally accessible variable" Follow @chewxy on Twi

Slide 7

Slide 7 text

_name A Gotcha: In mypkg.py: _x = "internal use only" x = "externally accessible variable" >>> from mypkg import * >>> x 'externally accessible variable' Follow @chewxy on Twi

Slide 8

Slide 8 text

_name A Gotcha: In mypkg.py: _x = "internal use only" x = "externally accessible variable" >>> from mypkg import * >>> x 'externally accessible variable' >>> _x Traceback (most recent call last): File "", line 1, in NameError: name '_x' is not defined Follow @chewxy on Twi

Slide 9

Slide 9 text

_name A Gotcha: In mypkg.py: _x = "internal use only" x = "externally accessible variable" >>> from mypkg import * >>> x 'externally accessible variable' >>> _x Traceback (most recent call last): File "", line 1, in NameError: name '_x' is not defined Avoid this! Follow @chewxy on Twi

Slide 10

Slide 10 text

__name DANGER ZONE Follow @chewxy on Twi

Slide 11

Slide 11 text

__name class Foo: def __init__(self, x): self.__x = x Follow @chewxy on Twi

Slide 12

Slide 12 text

__name class Foo: def __init__(self, x): self.__x = x >>> f = Foo(1) >>> f.__x Follow @chewxy on Twi

Slide 13

Slide 13 text

__name class Foo: def __init__(self, x): self.__x = x >>> f = Foo(1) >>> f.__x Traceback (most recent call last): File "", line 1, in AttributeError: 'Foo' object has no attribute '__x' >>> Follow @chewxy on Twi

Slide 14

Slide 14 text

__name class Foo: def __init__(self, x): self.__x = x >>> f = Foo(1) >>> f.__x Traceback (most recent call last): File "", line 1, in AttributeError: 'Foo' object has no attribute '__x' >>> WTF IS GOING ON?? Follow @chewxy on Twi

Slide 15

Slide 15 text

__name class Foo: def __init__(self, x): self.__x = x >>> f = Foo(1) >>> dir(f) ['_Foo__x', '__class__', '__dela>> Follow @chewxy on Twi

Slide 16

Slide 16 text

__name class Foo: def __init__(self, x): self.__x = x >>> f = Foo(1) >>> dir(f) ['_Foo__x', '__class__', '__dela>> Follow @chewxy on Twi

Slide 17

Slide 17 text

__name class Foo: def __init__(self, x): self.__x = x >>> f = Foo(1) >>> f._Foo__x 1 Follow @chewxy on Twi

Slide 18

Slide 18 text

__name class Foo: def __init__(self, x): self.__x = x def get_x(self): return self.__x >>> f = Foo("SyPy") >>> f.get_x() 'SyPy' >>> f.__x Traceback (most recent call last): File "", line 1, in AttributeError: 'Foo' object has no attribute '__x' Follow @chewxy on Twi

Slide 19

Slide 19 text

__name class Foo: def __init__(self, x): self.__x = x >>> f = Foo(1) >>> dir(f) ['_Foo__x', '__class__', '__dela>> Name Mangling Follow @chewxy on Twi

Slide 20

Slide 20 text

Why Mangle Names? class Foo: def __init__(self, x): self.__x = x class Bar(Foo): def __init__(self, x): super().__init__(x+"1337") self.__x = x >>> b = Bar("SyPy") >>> dir(b) Follow @chewxy on Twi

Slide 21

Slide 21 text

Why Mangle Names? class Foo: def __init__(self, x): self.__x = x class Bar(Foo): def __init__(self, x): super().__init__(x+"1337") self.__x = x >>> b = Bar("SyPy") >>> dir(b) ['_Bar__x', '_Foo__x', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__'] Follow @chewxy on Twi

Slide 22

Slide 22 text

Why Mangle Names? class Foo: def __init__(self, x): self.__x = x class Bar(Foo): def __init__(self, x): super().__init__(x+"1337") self.__x = x >>> b = Bar("SyPy") >>> dir(b) ['_Bar__x', '_Foo__x', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__'] Follow @chewxy on Twi

Slide 23

Slide 23 text

Why Mangle Names? class Foo: def __init__(self, x): self.__x = x class Bar(Foo): def __init__(self, x): super().__init__(x+"1337") self.__x = x >>> b = Bar("SyPy") >>> b._Bar__x 'SyPy' >>> b._Foo__x 'SyPy1337' Follow @chewxy on Twi

Slide 24

Slide 24 text

Name Mangling NaughZness _Foo__x = 'SyPy still rocks' class Foo: def get_x(self): return __x >>> f = Foo() Follow @chewxy on Twi

Slide 25

Slide 25 text

Name Mangling NaughZness _Foo__x = 'SyPy still rocks' class Foo: def get_x(self): return __x >>> f = Foo() >>> f.get_x() 'SyPy still rocks' Follow @chewxy on Twi

Slide 26

Slide 26 text

Name Mangling NaughZness _Foo__x = 'SyPy still rocks' class Foo: def get_x(self): return __x >>> f = Foo() >>> f.get_x() 'SyPy still rocks' Python will mangle any name that starts with __ Follow @chewxy on Twi

Slide 27

Slide 27 text

__name__ Follow @chewxy on Twi

Slide 28

Slide 28 text

__name__ class Foo: def __init__(self, x): self.x = x def __add__(self, other): return self.x + other >>> f = Foo(1) >>> f + 1 2 Follow @chewxy on Twi

Slide 29

Slide 29 text

AutomaZc DifferenZaZon class Value(object): def __init__(self, v, d): self.v = v self.d = d def __mul__(self, other): other = promote(other) return Value(self.v * other.v, self.d*other.v + self.v*other.d) def __rmul__(self, other): return Constant(other) * self def __repr__(self): return repr(self.v) def promote(value): return value if isinstance(value, Value) else Value(value,1) Follow @chewxy on Twi

Slide 30

Slide 30 text

AutomaZc DifferenZaZon For context: d(x2)/dx = 2x >>> x = promote(3) >>> y = x * x >>> y 9 >>> y.d 6 With a bit more jiggering you get full proper automaZc parZal derivaZves Follow @chewxy on Twi

Slide 31

Slide 31 text

THE END Follow @chewxy on Twi