129
Vom Kompetenzmodell zum kompetenzorientierten Unterricht an Landwirtschaftlichen Fachschulen

Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

  • Upload
    others

  • View
    29

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

Whats and whys for

beginnersby Justas Trimailovas @ VilniusPy

github.com/trimailov [email protected]

Page 2: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

Myself and Python

learnt basics of C from university,programming seemed arduous

Page 3: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

Myself and Python

2 years ago discovered Python through Udacity’s MOOC

and I loved it!

Page 4: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

Myself and Python

little by little advancing through Python, Linux and growing interest in various technologies

Page 5: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

About this talk

“5 WTFs in Python”

Page 6: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

About this talk

answer questions raised by novices in Python (or in programming in general)

Page 7: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

About this talk

answer questions raised by novices in Python (or in programming in general)

novice me

Page 8: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

About this talk

answer questions raised by novices in Python (or in programming in general)

novice me

*questions are not in any particular order

Page 9: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

About this talk

things you can learn from a lazy guy, so you can improve yourself little faster

Page 10: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

Not in this talk

Python 101

How to setup your machine for Python development

vim, emacs, Sublime Text, PyCharm is best Python IDE

Page 11: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

Not in this talk

Python 101

How to setup your machine for Python development

vim, emacs, Sublime Text, PyCharm is best Python IDE

Page 12: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

Not in this talk

Python 101

How to setup your machine for Python development

vim, emacs, Sublime Text, PyCharm is best Python IDE

Page 13: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

Not in this talk

Python 101

How to setup your machine for Python development

vim, emacs, Sublime Text, PyCharm is best Python IDE

Page 14: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

Not in this talk

Python 101

How to setup your machine for Python development

vim, emacs, Sublime Text, PyCharm is best Python IDE

Though you can freely ask about all that later

Page 15: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

How can software have so many files?

Page 16: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

> tree . . └── first ├── baz.py ├── functions │ ├── __init__.py │ └── bar.py └── other_functions ├── __init__.py └── biz.py

WHAT?! empty?!

Page 17: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

> cat first/functions/bar.py def drink_beer(where): print(“fun @ %s” % where)

> cat first/baz.py

from functions import bar

bar.drink_beer(where=“snekutis”)

Page 18: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

What is self?

Page 19: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

class Character: def __init__(self, int=1, chr=1, str=1): self.int = int # intelligence self.chr = chr # charisma self.str = str # strength

def learn(self): self.int += 1

def socialize(self): self.chr += 1

def train(self): self.str += 1

Page 20: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

class Character: def __init__(self, int=1, chr=1, str=1): self.int = int # intelligence self.chr = chr # charisma self.str = str # strength

def learn(self): self.int += 1

def socialize(self): self.chr += 1

def train(self): self.str += 1

def get_self(self): print(self)

Page 21: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

>>> knight = Character() >>> knight.get_self() >>> <Character object at 0x106160a20>

self is an instance of a class

Page 22: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

Method

class Character: .. def learn(self): self.int += 1 ..

Function

.. def learn(character): character.int += 1 ..

Page 23: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

Instance of a method of is passed automatically, but not received

Page 24: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

Received

class Character: .. def learn(self): self.int += 1 ..

Passed

knight.learn()

Instance of a method of is passed automatically, but not received

Page 25: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

Received

class Character: .. def learn(self): self.int += 1 ..

Passed

knight.learn()

Instance of a method of is passed automatically, but not received

Page 26: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

Why so many __underscores__?

Page 27: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

It’s a Python convention used for builtin attributes and to avoid namespace conflicts

Page 28: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

More conventions exist

_internal_use

__very_private -> _classname__very_private

Page 29: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

>>> dir(knight) ['__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__', 'chr', 'int', 'learn', 'socialize', 'str', 'train']

Page 30: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

__call__ - method called on instance call __new__ - method called on instance creation (before __init__) __init__ - method for object initialization __dict__ - get attribute:value dictionary __doc__ - return docstring __eq__ - describes ‘==‘ operator __lt__ - describes ‘<‘ operator, etc.

https://docs.python.org/3.4/reference/datamodel.html

Page 31: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

What is super?

Page 32: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

class Knight(Character): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs)

self.race = “Human”

Page 33: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

Character: __init__, learn, socialize, train

Knight: __init__, learn, socialize, train

If super() would not have been used, child class would have overriden it’s parent’s __init__ method.

Then initialized Knight would have race, but no int, str or chr.

Page 34: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

super() let’s us call same method from parent class. This way we can very flexibly extend classes.

Character: __init__, learn, socialize, train

Knight: __init__, learn, socialize, train

super()

*Python allows multiple inheritance, same principles apply, though some cases can be trickier. Reading about Python’s Method Resolution Order (MRO) is a must.

Page 35: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

But what is this magic with *args and **kwargs?

Page 36: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

But what is this magic with *args and **kwargs?

*in my head it sounds like arcs and quarks

Page 37: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

* - unwraps positional arguments ** - unwraps keyword arguments

Page 38: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

* - unwraps positional arguments ** - unwraps keyword arguments

You can name them what ever you like, e.g. *arcs, **quarks. Though *a, **kw and similar are usual.

Page 39: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

Basically what it means, you can let function to accept arbitrary arguments

Page 40: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

class Knight(Character): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs)

self.race = “Human”

baby_knight_params = {‘int’: 0, ‘chr’: 5, ‘str’: 15}

knight = Knight(**baby_knight_params)

Page 41: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

class Character: def __init__(self, *args, **kwargs): .. self.hp = kwargs.get(“hp”, 50)

Page 42: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

def ugly_function(*args, **kwargs): do_ugly_stuff_with(args, kwargs)

def get_ugly_params(): random_args = call_api() sorted_args = sort_args(random_args)

params_as_dict = call_dict_api() ugly_function(*sorted_args, **params_as_dict)

When you’re lazy, as *args and **kwargs are just shorter

Page 43: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

What is yield?

Page 44: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

yield returns a generator object

Page 45: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

Function

def simple(): l = [] for i in range(5): l.append(i) return i

Generator

def simple(): for i in range(5): yield i

Page 46: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

Function

def simple(): l = [] for i in range(5): l.append(i) return i

Generator

def simple(): for i in range(5): yield i

Saves memory Lazy evaluation

Could be endless (RNG) generator.__next__()

Page 47: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

Tips and tricks

Page 48: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

What this code does?

class Knight(Character): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs)

self.race = “Human”

Page 49: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

Answer: good editor and ctags

good editor - can read tags files or generate them. By using shortcut, we can jump to class/method definition

ctags - software which indexes your code

Page 50: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

Bonus answer: ag, ack, grep

>>> ag ‘class Character’

Page 51: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

What this code does at runtime?

Answer: pdb and ipdb

import ipdb; ipdb.set_trace() or

import pdb; pdb.set_trace()

Page 52: Whats and whys for - VilniusPyvilniuspy.lt/slides/whats_and_whys_for_python_beginners_justas.pdf · vim, emacs, Sublime Text, PyCharm is best Python IDE. Not in this talk Python 101

Conclusions and recomendations

Hold your horses: read manuals, documentations, sources

Python and brief C knowledge let’s me appreciate and enjoy programming

Try to understand how things work in general way, no need to guess

Low level knowledge helps