Cerca

Heus ací els resultats de la cerca.

2.4 Decorators
135 Resultats, Darrera modificació:
e of related objects. Python is able to decorate functions, methods, and classes. The decorator's operation is based on wrapping the original function with a new "decorating" function (or class), hence the name "decoration". This is done by passing the original function (i.e., the **decorated** function) as a para
2.7 Encapsulation
86 Resultats, Darrera modificació:
ribute encapsulation Encapsulation is one of the fundamental concepts in object-oriented programming (... hism, and abstraction). It describes the idea of bundling attributes and methods that work on those at... inside a class like in a **capsule**, preventing unauthorized parties' direct access to them. Publicl... object. This can be a way to enforce a certain amount of privacy for the attributes. This picture pre
4.1 Shallow and deep copy operations
59 Resultats, Darrera modificació:
ject: label vs. identity vs. value; * the id() function and the is operand; * shallow and deep cop... ithout making use of variables. As variables are fundamental elements that allow us to cope with objec... lue and label not enough? The built-in ''id()'' function returns the 'identity' of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifet
2.3 Extended function argument syntax
35 Resultats, Darrera modificació:
= 2.3 Extended function argument syntax When we talk about function arguments, we should recall the following facts: * some functions can be invoked without arguments; * functions may require a specific number of arguments with
5.1 Metaprogramming
35 Resultats, Darrera modificació:
dify their own or other programs’ codes. It may sound like an idea from a science fiction story, but t... e'll touch on the “deeper magic” in a benign way. Understanding Python metaclasses is worthwhile, because it leads to a better understanding of what is happening under Python's hood when classes are created. In Python, a metaclass
4.2 Serialization of Python objects using the pickle module
32 Resultats, Darrera modificació:
of Pythonistas. So, what can be pickled and then unpickled? The following types can be pickled: *... ts (remember to avoid cycles!) * references to functions and classes, but not their definitions. Le... ning a string, an integer, and a list. When you run the code presented in the right pane, a new file should be created. Remember to run the code locally. <code python> import pickle a_
2.9 Inheriting properties from built-in classes
28 Resultats, Darrera modificació:
class has the advantage of all of the well-known functionalities inherited from its parent or even par... to collect the serial numbers of sold tickets. Sound reasonable enough? Your new class will be bas... _setitem__%%'', which is a magic method (mind the underscores) responsible for inserting (overwriting)... red? * All the remaining methods have remained unchanged, so our new list-like class will still beh
2.1 Python core syntax
27 Resultats, Darrera modificació:
respective data types. Now, we'll use the same function ''len()'' to get a number of elements of a t... **the same** operators or instructions, or even functions. Python core syntax covers: * operators... * indexing, slicing, subscripting; * built-in functions like str(), len() * reflexion – isinstanc... or should allow them to be lifted. When called, functions and operators that state the core syntax ar
2.5 Different faces of Python methods
24 Resultats, Darrera modificació:
= 2.5 Different faces of Python methods Until now, we’ve been implementing methods that have performed... ods. These alternative types of method should be understood as tool methods, extending our ability to... instantiated. You can say that they are methods bound to the class, not to the object. When can this ... r. <code python> class Example: __internal_counter = 0 def __init__(self, value): Ex
2.2 Inheritance and polymorphism — Inheritance as a pillar of OOP
21 Resultats, Darrera modificació:
nce as a pillar of OOP Inheritance is one of the fundamental concepts of object oriented programming, and expresses the fundamental relationships between classes: superclass... heritance creates a class hierarchy. Any object bound to a specific level of class hierarchy inherits ... nce class is always simpler, safer, and easier to understand and maintain; * multiple inheritance ma
1.2 Working with class and instance data – instance variables
20 Resultats, Darrera modificació:
e – remember this in order to avoid wasting time hunting for bugs! The snippet contains additional co... low demonstrates both topics: each class owns a counter variable that holds the number of class instan... lps identify the class instance origins. Similar functionality could be achieved with the ''isinstance()'' function, but we want to check if class variables can
1.1 Classes, Instances, Attributes, Methods — introduction
17 Resultats, Darrera modificació:
miliar with the basics of OOP, so to establish an understanding of common terms, we should agree on th... t; could be a variable or method; * method — a function built into a class that is executed on behal... remember that in Python everything is an object (functions, modules, lists, etc.). In the very last se... tual buildings. Classes describe attributes and functionalities together to represent an idea as accu
3.1 Advanced techniques of creating and serving exceptions
16 Resultats, Darrera modificació:
ntroduction When Python executes a script and encounters a situation that it cannot cope with, it: ... the fact that all potential exceptions have their unambiguous names, so you can categorize them and re... try: problematic_code'' except code block to surround the "problematic" piece of code. In effect, when... ld not trust the data types, so it’s wise to surround the fragile code (int() in this example) with a
2.6 Abstract classes
10 Resultats, Darrera modificació:
an that there are no controls to impose a set of functionalities or an order in a class hierarchy. Whe... __ abstract methods. When we’re designing large functional units, in the form of classes, we should use an abstract class. When we want to provide common implemented functionality for all implementations of the class, w
2.8 Composition vs Inheritance - two ways to the same destination
6 Resultats, Darrera modificació:
e is a great concept, one of the most important foundations of object-oriented programming that models... ture of classes. This hierarchy would be hard to understand, debug, and extend. This phenomenon is kn... could be exchanged any time, even during program runtime. Let's try to write some code to see how com... ethod) to make it work in a polymorphic manner. Run the code to confirm your expectations. <code ; o
4.3 Making Python objects persistent using the shelve module
6 Resultats, Darrera modificació: