Korištenje dekoratora je jednostavno, no pisanje zna biti kompleksno.
• Kroz jednostavne korake ćemo proći i naučiti dekoratore
1. Funkcije
Kreiranje def, parametri, vraćanje vrijednosti, poziv funkcije
def foo():
return 1
print foo()
1
2. Scope
Namespace funkcije za identificiranje varijabli u tijelu funkcije
a_string = "This is a global variable"
def foo():
print a_string # 1
foo()
This is a global variable
3. variable resolution rules
Pristup globalnim varijablama (promjenjiv tip podataka podržava promjene)
a = ["Pero"]
b = "Pero”
def foo():
a[0] = "Ivo”
b = "Ivo"
print locals()
foo()
print globals()
print a
4. Variable lifetime
Namespace je svaki puta kreiran i uništen prilikom poziva funkcije, ne postoji sintaksa za vrijednost varijable.
def foo():
x = 1
foo()
5. Function arguments and parameters
Parametri funkcije mogu biti nazivi ili pozicije
def foo(x, y=0):
return x - y
print foo(3,1)
print foo(3)
6. Nested function
Python gleda scope outer prvo i pronalazi lokalnu varijablu kroz inner
def outer():
x = 1
def inner():
print x
inner()
outer()
7. Functions are first class objects in Python
Funkcije su objekti u Pythonu, (klase su također objekti), shvaćamo ih kao vrijednosti, te ih možemo koristiti npr. kao argumente.
print issubclass(int, object)
def foo():
pass
print foo.__class__
print issubclass(foo.__class__, object)
8. Closures
Inner funkcija definirana u ne globalnom scope pamti izgled namespace.
def outer():
x = 1
def inner():
print x
return inner
foo = outer()
foo()
9. Decorators
def outer(some_func):
def inner():
print "Before some_func"
ret = some_func()
print some_func.__name__
return ret + 1
return inner
def foo():
return 1
decorated = outer(foo)
print decorated()
10. Symbol @ applies a decorator
def outer(some_func):
def inner():
print "Before some_func"
ret = some_func()
print some_func.__name__
return ret + 1
return inner
@outer
def foo():
return 1
print foo()
11. *args and **kwargs
def logger(func):
def inner(*args, **kwargs):
print "Argumenti su bili : %s, %s" %(args, kwargs)
return func(*args, **kwargs)
return inner
@logger
def fool(x,y=1):
return x * y
@logger
def fool2():
return 2
print fool(5,2)
print fool2()
12. functools.wraps
from functools import wraps
def logged(func):
@wraps(func)
def with_logging(*args, **kwargs):
print func.__name__ + " was called"
return func(*args, **kwargs)
return with_logging
@logged
def f(x):
"""does some math"""
return x + x * x
print f(3)
print f.__name__
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016Loïc Knuchel
Vous commencez à en entendre parler de plus en plus mais vous avez encore du mal à voir ce que c’est et à comprendre de que ça change concrètement, ce talk est fait pour vous !!!
La programmation fonctionnelle est une manière de programmer basée sur les fonctions qui permet de faire du code vraiment modulaire, améliorer la qualité et limiter les bugs. Vous ne me croyez pas ? Venez voir cette session !
Korištenje dekoratora je jednostavno, no pisanje zna biti kompleksno.
• Kroz jednostavne korake ćemo proći i naučiti dekoratore
1. Funkcije
Kreiranje def, parametri, vraćanje vrijednosti, poziv funkcije
def foo():
return 1
print foo()
1
2. Scope
Namespace funkcije za identificiranje varijabli u tijelu funkcije
a_string = "This is a global variable"
def foo():
print a_string # 1
foo()
This is a global variable
3. variable resolution rules
Pristup globalnim varijablama (promjenjiv tip podataka podržava promjene)
a = ["Pero"]
b = "Pero”
def foo():
a[0] = "Ivo”
b = "Ivo"
print locals()
foo()
print globals()
print a
4. Variable lifetime
Namespace je svaki puta kreiran i uništen prilikom poziva funkcije, ne postoji sintaksa za vrijednost varijable.
def foo():
x = 1
foo()
5. Function arguments and parameters
Parametri funkcije mogu biti nazivi ili pozicije
def foo(x, y=0):
return x - y
print foo(3,1)
print foo(3)
6. Nested function
Python gleda scope outer prvo i pronalazi lokalnu varijablu kroz inner
def outer():
x = 1
def inner():
print x
inner()
outer()
7. Functions are first class objects in Python
Funkcije su objekti u Pythonu, (klase su također objekti), shvaćamo ih kao vrijednosti, te ih možemo koristiti npr. kao argumente.
print issubclass(int, object)
def foo():
pass
print foo.__class__
print issubclass(foo.__class__, object)
8. Closures
Inner funkcija definirana u ne globalnom scope pamti izgled namespace.
def outer():
x = 1
def inner():
print x
return inner
foo = outer()
foo()
9. Decorators
def outer(some_func):
def inner():
print "Before some_func"
ret = some_func()
print some_func.__name__
return ret + 1
return inner
def foo():
return 1
decorated = outer(foo)
print decorated()
10. Symbol @ applies a decorator
def outer(some_func):
def inner():
print "Before some_func"
ret = some_func()
print some_func.__name__
return ret + 1
return inner
@outer
def foo():
return 1
print foo()
11. *args and **kwargs
def logger(func):
def inner(*args, **kwargs):
print "Argumenti su bili : %s, %s" %(args, kwargs)
return func(*args, **kwargs)
return inner
@logger
def fool(x,y=1):
return x * y
@logger
def fool2():
return 2
print fool(5,2)
print fool2()
12. functools.wraps
from functools import wraps
def logged(func):
@wraps(func)
def with_logging(*args, **kwargs):
print func.__name__ + " was called"
return func(*args, **kwargs)
return with_logging
@logged
def f(x):
"""does some math"""
return x + x * x
print f(3)
print f.__name__
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016Loïc Knuchel
Vous commencez à en entendre parler de plus en plus mais vous avez encore du mal à voir ce que c’est et à comprendre de que ça change concrètement, ce talk est fait pour vous !!!
La programmation fonctionnelle est une manière de programmer basée sur les fonctions qui permet de faire du code vraiment modulaire, améliorer la qualité et limiter les bugs. Vous ne me croyez pas ? Venez voir cette session !
The document discusses the benefits of exercise for mental health. Regular physical activity can help reduce anxiety and depression and improve mood and cognitive functioning. Exercise causes chemical changes in the brain that may help protect against mental illness and improve symptoms.
The document discusses the benefits of exercise for mental health. Regular physical activity can help reduce anxiety and depression and improve mood and cognitive functioning. Exercise causes chemical changes in the brain that may help protect against mental illness and improve symptoms.
The document discusses the results of a study on the effects of a new drug on memory and cognitive function in older adults. The double-blind study involved giving either the new drug or a placebo to 100 volunteers aged 65-80 over a 6 month period. Testing showed those receiving the drug experienced statistically significant improvements in short-term memory retention and processing speed compared to the placebo group.
This document discusses strings and objects in JavaScript. It defines strings as text enclosed in double or single quotes. It also defines objects as collections of key-value pairs enclosed in curly braces. The document then lists common string and object methods like length, indexOf, split, contains for strings and accessing properties and methods for objects. It also covers the this keyword, scope, and differences between primitive and reference types in JavaScript.
This document discusses the structure and functionality of databases, back-ends, and front-ends. It also covers the evolution of HTML standards from 1995 to 1999 and JavaScript syntax elements such as variables, data types, operators, and control flow statements like conditionals and loops.
Emmet is a plugin that improves HTML and CSS workflow by allowing users to quickly generate code through abbreviations. It can be installed for various IDEs through the Emmet website. Emmet uses syntax like div#header to generate <div id="header"></div> and div*3 to create three <div></div> elements at once. It also supports nesting elements, adding text, and inserting attributes to greatly simply the process of writing markup.
The document discusses various new semantic HTML5 elements such as <header>, <footer>, <nav>, <article>, <section>, <aside>, <figure>, <figcaption>, <time>, <video>, and <audio>. It provides examples of how to properly use each element and brief descriptions of what each element represents and when it would be appropriate to use. The key changes in HTML5 are the introduction of these new semantic elements which allow marking up sections of a document in a way that conveys meaning rather than just presentation.
LESS and Sass are CSS preprocessors that allow for variables, mixins, and nested rules to make CSS more maintainable. Some key differences are that LESS uses @variables while Sass uses $variables. Both support mixins but LESS calls them with .mixin() while Sass uses @include. Nesting rules in LESS and Sass avoids repetition by nesting selectors within parent elements. Both support math operations and functions to generate CSS values programmatically.
CSS selectors allow styling of HTML elements based on their type, id, class, and attributes. The main selectors include the universal, type, id, class, attribute, child, sibling, and combinator selectors. CSS rules define the properties and values to apply to the selected elements.
This document describes various CSS pseudo-class and pseudo-element selectors including: link-related selectors like :link, :visited, :active, :hover; input-related selectors like :focus, :target, :enabled, :disabled, :checked; position-based selectors like :root, :first-child, :last-child, :nth-child; relational selectors like :not; text-related selectors/elements like :first-letter, :first-line, :before, :after; and how each can be used to select and style elements.
The document discusses the benefits of exercise for mental health. Regular physical activity can help reduce anxiety and depression and improve mood and cognitive functioning. Exercise causes chemical changes in the brain that may help protect against mental illness and improve symptoms.
The document discusses the benefits of exercise for mental health. Regular physical activity can help reduce anxiety and depression and improve mood and cognitive functioning. Exercise causes chemical changes in the brain that may help protect against mental illness and improve symptoms.
The document discusses the results of a study on the effects of a new drug on memory and cognitive function in older adults. The double-blind study involved giving either the new drug or a placebo to 100 volunteers aged 65-80 over a 6 month period. Testing showed those receiving the drug experienced statistically significant improvements in short-term memory retention and processing speed compared to the placebo group.
This document discusses strings and objects in JavaScript. It defines strings as text enclosed in double or single quotes. It also defines objects as collections of key-value pairs enclosed in curly braces. The document then lists common string and object methods like length, indexOf, split, contains for strings and accessing properties and methods for objects. It also covers the this keyword, scope, and differences between primitive and reference types in JavaScript.
This document discusses the structure and functionality of databases, back-ends, and front-ends. It also covers the evolution of HTML standards from 1995 to 1999 and JavaScript syntax elements such as variables, data types, operators, and control flow statements like conditionals and loops.
Emmet is a plugin that improves HTML and CSS workflow by allowing users to quickly generate code through abbreviations. It can be installed for various IDEs through the Emmet website. Emmet uses syntax like div#header to generate <div id="header"></div> and div*3 to create three <div></div> elements at once. It also supports nesting elements, adding text, and inserting attributes to greatly simply the process of writing markup.
The document discusses various new semantic HTML5 elements such as <header>, <footer>, <nav>, <article>, <section>, <aside>, <figure>, <figcaption>, <time>, <video>, and <audio>. It provides examples of how to properly use each element and brief descriptions of what each element represents and when it would be appropriate to use. The key changes in HTML5 are the introduction of these new semantic elements which allow marking up sections of a document in a way that conveys meaning rather than just presentation.
LESS and Sass are CSS preprocessors that allow for variables, mixins, and nested rules to make CSS more maintainable. Some key differences are that LESS uses @variables while Sass uses $variables. Both support mixins but LESS calls them with .mixin() while Sass uses @include. Nesting rules in LESS and Sass avoids repetition by nesting selectors within parent elements. Both support math operations and functions to generate CSS values programmatically.
CSS selectors allow styling of HTML elements based on their type, id, class, and attributes. The main selectors include the universal, type, id, class, attribute, child, sibling, and combinator selectors. CSS rules define the properties and values to apply to the selected elements.
This document describes various CSS pseudo-class and pseudo-element selectors including: link-related selectors like :link, :visited, :active, :hover; input-related selectors like :focus, :target, :enabled, :disabled, :checked; position-based selectors like :root, :first-child, :last-child, :nth-child; relational selectors like :not; text-related selectors/elements like :first-letter, :first-line, :before, :after; and how each can be used to select and style elements.