python

 0    20 fiche    patrykdastych
Imprimer jouer consultez
 
question - réponse -
python naming conv
commencer à apprendre
function variables: snake-case class: CamelCase
MY_CONSTANT, module my_module, package mypackage(dont use _)
metaprogramming
commencer à apprendre
potential for a program to have knowledge of or manipulate itself
language primitive
commencer à apprendre
you work on it with assembler, it is smallest "unit of processing" available
high level languages as python dont use them, python suffers abstraction penalty which is penalty for having various features
object vs instance
commencer à apprendre
instance is an object that is built from class
object is an instance of class
instance attributes vs class attributes
commencer à apprendre
instance are different in instances and class attributes have the same value in every instance
inheritance
commencer à apprendre
mechanizm dzielenia funkcjonalnosci miedzy klasami
it is a process where one class take attributes and methods of another class
isinstance()
commencer à apprendre
built-in function
isinstance(instance, Class) returns true if instance is instance of Class. it also would return true if Class would be parent Class(inherited)
subclass vs superclass
commencer à apprendre
subclass (child) the class that inherits from another class
superclass (parent) the class being inherited from
super()
commencer à apprendre
returns temporary object of the superclass allowing calling its methods
user in inheritance
_var __var
commencer à apprendre
its a mechanism, naming convetion _var tells programmer that this variable shouldnt be user outside of the class but you can access; object. _var(its just naming convention)
EVERYTHING IN PYTHON IS PUBLIC
__var(mechanism of hiding attribute) in class you just use attribute with __var but outside of class its name is changed(hidden) you can still access attribute but its name is: _MyClassName__var
property
commencer à apprendre
in object oriented languages is a special sort of class member, in functionality between field(attribute) and methods. In python property is class but is called a function
@propery def length(self): return self. _length. @length. setter def length(self, length): self. _length=length
Class, type
commencer à apprendre
If we define class: Class Foo: pass, then it is actually an object created by an object type: type('Foo', (), {}) it is the same
type is a class of a class
metaclass
commencer à apprendre
metaclass is above the class that is created, any class that is created is an instance of the "type" metaclass
Class Foo(metaclass=type) nic nie zmiena bo metaclass kazdej klasy jest domyslnie type, type jest klasa dla klasy a slowo Class to zwykly syntax xd
mutable types as class attribute
commencer à apprendre
Class Foo: x=[]
mutable data types: list, dict, set, class instances
Jesli damy x. append(1) to dodamy 1 we wszystkich instancjach klasy Foo xD
python scope
commencer à apprendre
LEGB rule: local(function) scope, enclosing(nonlocal) scope, global(or module) scope, built-in scope
python attributes
commencer à apprendre
are evaluated on declaration(or import)
asgi, wsgi
commencer à apprendre
asynchronous server gateway interface(or web)
asgi is successor, it is better
assert vs raise
commencer à apprendre
assert do debugowania, raise rzuca exception, assert mozna wylaczyc flaga -o
if x==0: raise Exception("xD")
assert x!=0, "Cannot divide by 0"
exceptions
commencer à apprendre
raise - throws exception at any time; assert - checks if certain condition is met, throws exception if it is not(by default AssertionError but can be changed)
else - do smth if no exceptions encountered in try;. finally - do smth (doesnt matter if exception happened or not)
try - all statements executed until exception encountered;. except - catch and handle exception(ex. Except AssertionError:) bad idea to except without any error(Except:)
deep copy shallow copy
commencer à apprendre
deep - makes fully independent object
immutable data types doesnt really have difference between deep and shallow

Vous devez vous connecter pour poster un commentaire.