Welcome | Get started | Dive | Contribute | Topics | Reference | Changes | More
Python crash courses¶
This section is for people who know the basics of Python but who are not yet experienced experts. It quickly explains certain things you need to know because Lino uses them.
What is sys.argv¶
(1) Consider the following source code:
import sys
print(sys.argv) # a list
s = ''
for i in sys.argv:
s += i
print(s)
(2) Open your copy of this file in your editor:
$ go book
$ pulsar docs/dev/newbies/py2lino/1.py
What is os.environ¶
(3) Consider the following source code:
mo = {"a": 2, "b": 2, "c": 6}
print(type(mo))
print(mo['a'])
import os
print(type(os.environ))
print(os.environ['PATH'])
(4) Open your copy of this file in your editor:
$ go book
$ pulsar docs/dev/newbies/py2lino/2.py
mo = {"a": 2, "b": 2, "c": 6}
print(type(mo))
print(mo['a'])
import os
print(type(os.environ))
print(os.environ['PATH'])
Classes and instances¶
(5) Consider the following source code:
class Vehicle: # a class
number_of_wheels = None # a class attribute
max_speed = 20
def get_speed(self): # a method
"""Return the maximum speed."""
return self.max_speed
def get_foo(self): # a method
raise NotImplementedError
class Bike(Vehicle): # a subclass, inherts from Vehicle, extends Vehicle
number_of_wheels = 2
class Car(Vehicle):
brand = None
number_of_wheels = 4
max_speed = 90
class Benz(Car):
star_logo = True
brand = "Mercedes"
class OldBenz(Benz):
star_logo = False
def get_speed(self):
return super().get_speed() - 20
# super_object = super(OldBenz, self)
# print(type(super_object))
# meth = super_object.get_speed
# speed = meth()
# return speed - 20
class VW(Car):
brand = "Volkswagen"
diesel = False
(6) Open your copy of this file in your editor:
$ go book
$ pulsar docs/dev/newbies/py2lino/c3.py
(7) Open a terminal and say:
$ cd docs/dev/newbies/py2lino
$ python
(8) You are now in a Python prompt. Try the following commands.
>>> from c3 import *
>>> x = OldBenz() # we instantiate an OldBenz
>>> print(x.number_of_wheels)
4
>>> print(x.get_speed())
70
>>> print(x.star_logo)
False
>>> y = OldBenz() # another instance of OldBenz
>>> y.max_speed = 100
>>> print(y.get_speed())
80
>>> print(x.get_speed())
70
(9) We now have two instances of OldBenz, one of them is a bit faster. But they are still OldBenz instances.
>>> OldBenz
<class 'c3.OldBenz'>
>>> x
<c3.OldBenz object at ...>
>>> isinstance(x, OldBenz)
True
>>> type(x)
<class 'c3.OldBenz'>
>>> x.__class__
<class 'c3.OldBenz'>
(10) We say that OldBenz
is a
class object, while x
and y
are instances of it.
- class object¶
An object that represents a class. Calling a class object will return an instance of this class. Class objects are themselves instances of
type
.
>>> my = VW()
>>> print(my.number_of_wheels)
4
>>> print(my.diesel)
False
>>> print(my.star_logo)
Traceback (most recent call last):
...
AttributeError: 'VW' object has no attribute 'star_logo'
What means “yield”¶
(11) Consider the following source code:
""" generator functions
>>> print(f1())
[0, 1, ..., 999]
>>> print(f2())
<generator object f2 at ...>
"""
def f1():
l = []
for i in range(1000):
l.append(i)
return l
def f2():
for i in range(1000):
yield i # yield ulatama
# for i in f1():
# print i
# # to iterate =
# for i in f2():
# print i
if __name__ == '__main__':
import doctest
doctest.testmod(optionflags=doctest.ELLIPSIS)
(12) Open your copy of this file in your editor:
$ go book
$ pulsar docs/dev/newbies/py2lino/4.py