Cerca
Heus ací els resultats de la cerca.
Resultats de text complet:
- 2.7 Encapsulation
- inside a class like in a **capsule**, preventing unauthorized parties' direct access to them. Publicl... ation does not have to be changed; by consumer we understand someone or something (it could be a legac... Attribute encapsulation can be also used to limit unauthorized access: reading and modifying the accou... ributes marked as private (prefixed with a double underscore) in a direct way, then remember that this
- 5.1 Metaprogramming
- 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... t creating our own metaclasses, it’s important to understand some more details regarding classes and t
- 4.2 Serialization of Python objects using the pickle module
- of Pythonistas. So, what can be pickled and then unpickled? The following types can be pickled: *... .dump(a_list, file_out) </code> Now it’s time to unpickle the contents of the file. <code python> im... named reference, so the same restrictions in the unpickling environment apply. Note that none of the ... at the environment where the class or function is unpickled is able to import the class or function de
- 3.1 Advanced techniques of creating and serving exceptions
- the fact that all potential exceptions have their unambiguous names, so you can categorize them and re... 'abcdefghijk'",) abcdefghijk None </code> The ''UnicodeError'' exception – raised when a Unicode-related encoding or decoding error occurs. It is a subclass of ValueError. The UnicodeError has attributes that describe an encodin
- 2.3 Extended function argument syntax
- ted arguments. In other words, *args collects all unmatched positional arguments; * **%%**%%kwargs**... eyword arguments) – refers to a dictionary of all unexpected arguments that were passed in the form of... =value pairs. Likewise, %%**%%kwargs collects all unmatched keyword arguments. In Python, asterisks a... ameters are not ordinary parameters and should be unpacked, as they carry multiple items. If you’ve e
- 2.4 Decorators
- er. So from now on, the term 'decorator' will be understood as a decorating class or a decorating fun... simple function! </code> == Decorators should be universal Consider a function that accepts arguments... ld also be decorated. Decorators, which should be universal, must support any function, regardless of ... ted in this way has become much more flexible and universal than 'simple_decorator', because it can ha
- 1.2 Working with class and instance data – instance variables
- s variable named **species**, which holds a value unique to each class. When we iterate over all objec... but the total weight limitation cannot exceed 300 units. Write a code that creates objects representi... n vary between 0.2 and 0.5 of an imaginary weight unit; Hint: Use a ''random.uniform(lower, upper)'' function to create a random number between the lower
- 2.1 Python core syntax
- name of each magic method is surrounded by double underscores (Pythonistas would say “dunder” for double underscores, as it’s a shorter and more convenient p... 0.py", line 11, in print(p1 + p2) TypeError: unsupported operand type(s) for +: 'Person' and 'Per... ic method counterparts. Treat it as a list from a universal map, unrelated to any special data type. {
- 2.2 Inheritance and polymorphism — Inheritance as a pillar of OOP
- nce class is always simpler, safer, and easier to understand and maintain; * multiple inheritance ma... specific types in order to treat those types in a uniform way. Imagine that you have to print a strin... * the code is clearly structured; * there is a uniform way of calling methods responsible for the s... ks to duck typing, we can call the melt() method. Unfortunatelly, the Wood class is not equipped with
- 4.1 Shallow and deep copy operations
- ect. This is an integer which is guaranteed to be unique and constant for this object during its lifet... ''id'' shadows the genuine function and makes it unreachable in the scope in which the variable has b... about making use of polymorphism when you need a universal function to copy any type object, so that ... es for the candies whose total weight exceeds 300 units of weight (we don’t care whether those are kil
- 1.1 Classes, Instances, Attributes, Methods — introduction
- miliar with the basics of OOP, so to establish an understanding of common terms, we should agree on th... simple: we have one class of a given type and an unlimited number of instances of a given class. Eac... butes' of Python objects. By 'callable' we should understand anything that can be called; such objects
- 2.6 Abstract classes
- t methods. When we’re designing large functional units, in the form of classes, we should use an abst... def hello(self): print('Nothing is blue unless you need it') bp = BluePrint() bp.hello() </c... s a clear output: <code ; output> Nothing is blue unless you need it </code> Python has come up with
- 2.9 Inheriting properties from built-in classes
- _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... 3166-1 standard (e.g., FR for France, GB for the United Kingdom, DE for Germany, and so on) * two c
- 2.5 Different faces of Python methods
- = 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
- 4.3 Making Python objects persistent using the shelve module
- y. The keys must be ordinary strings, because the underlying database (dbm) requires strings. Therefo... by pickle, it isn’t safe to load a shelve from an untrusted source. As with pickle, loading a shelve c