python

 0    20 Datenblatt    patrykdastych
Drucken spielen überprüfen
 
Frage - Antworten -
python naming conv
Lernen beginnen
function variables: snake-case class: CamelCase
MY_CONSTANT, module my_module, package mypackage(dont use _)
metaprogramming
Lernen beginnen
potential for a program to have knowledge of or manipulate itself
language primitive
Lernen beginnen
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
Lernen beginnen
instance is an object that is built from class
object is an instance of class
instance attributes vs class attributes
Lernen beginnen
instance are different in instances and class attributes have the same value in every instance
inheritance
Lernen beginnen
mechanizm dzielenia funkcjonalnosci miedzy klasami
it is a process where one class take attributes and methods of another class
isinstance()
Lernen beginnen
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
Lernen beginnen
subclass (child) the class that inherits from another class
superclass (parent) the class being inherited from
super()
Lernen beginnen
returns temporary object of the superclass allowing calling its methods
user in inheritance
_var __var
Lernen beginnen
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
Lernen beginnen
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
Lernen beginnen
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
Lernen beginnen
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
Lernen beginnen
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
Lernen beginnen
LEGB rule: local(function) scope, enclosing(nonlocal) scope, global(or module) scope, built-in scope
python attributes
Lernen beginnen
are evaluated on declaration(or import)
asgi, wsgi
Lernen beginnen
asynchronous server gateway interface(or web)
asgi is successor, it is better
assert vs raise
Lernen beginnen
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
Lernen beginnen
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
Lernen beginnen
deep - makes fully independent object
immutable data types doesnt really have difference between deep and shallow

Sie müssen eingeloggt sein, um einen Kommentar zu schreiben.