summaryrefslogtreecommitdiff
path: root/cs/Python.md
blob: bc08ed23e511fed78865772c30fff9411e36098f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
Python does not have a classpath and unless you are root, you can't install new pkg for the whole system

# pip installs in

.local/lib/


virtualenv - solution to pkg management? - isolated space with own instance of python

virtualenv TESTENV -p python
source TESTENV/bin/activate
# to get out of env
deactivate

** exponant

x = 'Mark'
len(x) - function
x.upper() - method

dir() - built it give directory of an object (all methods available)
with no arg show you all name in global namespace

dir() == sorted(globals().keys())

gloabls() == locals()

super() - goes trought inheritance chain and return the class that is after the one passed as the function first argument. Can by used to bypass modification made by earlier classes.


# Data containers

list()  - []
dict()  - {}
tuple() - ()
set()   - set()



__ init__  - where to assign instance specific attributes when object first constructed
__ str__ - control how object is printed
__ repr__ - object output when interactive shell
__ eq__ - uniquely identify all created object


1st time you import a modules python execute the code inside it.
2nd time will not re-run the code. (usefull when import multiples modules that import all the same modules)
importlib if want to reload modules so run more than once

When importing a module you can use it's defined functions, classes, constants etc...
Access those thing by putting the module name as prefix before the function

import X
import X as x
from X import x
from X import x as x

when you run a modules as a script __ name__ becomes __ main__


__ init__.py - required to tell python dir is a pkg (initialise a pkg) REQUIRED
used to set the __ all__ variable & import submodules