Cerca
Heus ací els resultats de la cerca.
Resultats de text complet:
- 2.4 Decorators
- dose of theory. So, let's create a function – ''simple_hello()'' is one of the simplest functions we could think of. We'll decorate it in a moment. <code python> def simple_hello(): print("Hello from simple function!") </code> <code python> def simple_decorator(fun
- 2.2 Inheritance and polymorphism — Inheritance as a pillar of OOP
- ython-pcpp1:pasted:20231013-035718.png }} A very simple example of two-level inheritance is presented... cle** classes. == Inheritance and polymorphism — Single inheritance vs. multiple inheritance There ar... nheritance should be used with more prudence than single inheritance because: * a single inheritance class is always simpler, safer, and easier to unde
- 2.8 Composition vs Inheritance - two ways to the same destination
- ance is to reuse the code. If two classes perform similar tasks, we can create a common base class for... f constructing adaptable objects. You can achieve similar goals by using a concept named composition. ... e code to see how composition works. Look at the simple code presented in the editor pane. <code pyt... et_pressure()'', ''pump()''; attribute available: size * engine; methods available: ''start()'', '
- 4.2 Serialization of Python objects using the pickle module
- utput of your data processing for later use? The simplest way to persist outcomes is to generate a fl... text file and to write your outcomes. It’s a very simple thing to do way which is not suitable for per... rint(data2) </code> The presented code is quite simple: * we’re importing a pickle module; * th... ct, returns the initial object. An example of in situ serialization and deserialization is presented
- 4.1 Shallow and deep copy operations
- atement is being used, so evaluation of the right side of the clause takes precedence over the left side.) * At first, an object (a list in this exampl... code or to experiment while copying objects. The side effect of this infrequent use is that some deve... s been defined. You should remember to avoid such situations! When you have two variables referring t
- 2.1 Python core syntax
- tors on our objects. Let's imagine the following situation: you've created a class that represents a... rm the appropriate action. Look at the following simple code: <code python>number = 10 print(number +... _', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__'... ic methods. The following tables will help you in situations where you'd like to implement a custom me
- 2.9 Inheriting properties from built-in classes
- validated value at a given position). Now you can sigh – “oh, what a great ability!” * an overridden... d__%%''() These implementations should be fairly similar to the implementations delivered above (vali... ** (International Bank Account Number) provides a simple and fairly reliable method of validating the account numbers against simple typos that can occur during rewriting of the
- 3.1 Advanced techniques of creating and serving exceptions
- on When Python executes a script and encounters a situation that it cannot cope with, it: * stops y... ' to effectively deal with exceptions. Imagine a situation where you are already handling an exceptio... translate it to another type of exception. Such a situation is typical when you have a good reason for... the unifying behavior of one piece of code to act similarly to another piece of code, like a legacy co
- 2.5 Different faces of Python methods
- ss method from an instance method, the programmer signals it with the ''@classmethod'' decorator prece... ernal()'' method is a class method. This has been signaled to the Python interpreter by using an appro... a class method or instance method, the programmer signals it with the ''@staticmethod'' decorator prec... ber before the object is created? To shorten the size of the sample code, the static method responsib
- 1.2 Working with class and instance data – instance variables
- ce is created. To get access to a class variable, simply access it using the class name, and then prov... rint(Demo.class_var) print(Demo.__dict__) </code> Similarly to instance variables, class variables are... n that helps identify the class instance origins. Similar functionality could be achieved with the ''i
- 2.6 Abstract classes
- bclasses. This capability is especially useful in situations where your team or third-party is going t... does such a common name as “abc” conflict with my simple module “abc”? Run your own experiment to bec... the error messages you would encounter in such a situation. == LAB === Objectives * Creation of ab
- 2.7 Encapsulation
- our class implementation from a class that allows simple and direct access to attributes to a class th... to set liquid level to 21 units, result:', e) # similar example - let's try to add an additional 15 ... t exception, * Implement a class representing a single bank account, * This class should control a
- 5.1 Metaprogramming
- the class and state the class namespace. A very simple example, when both ''bases'' and ''dictionary... ict__) </code> As a result, we have created the simple class “Dog”. <code ; output> The class name ... classes, because many problems can be solved in a simpler way than by using metaclasses. But still, i
- 1.1 Classes, Instances, Attributes, Methods — introduction
- a particular instance of a class. It’s a bit of a simplification, because the term object is more gene... e relation between instances and classes is quite simple: we have one class of a given type and an unl
- 2.3 Extended function argument syntax
- , 34, 23, 2) print(a_list) print(b_list) </code> Similarly to the **print()** function, the **list()*... ce type, because the order matters in this case). Similarly, all unexpected keyword parameters are ava