Cerca
Heus ací els resultats de la cerca.
Resultats de text complet:
- 3.1 Advanced techniques of creating and serving exceptions
- raised) when it has no idea what do to with your code. What happens next? * the raised exception ex... t an exception error message like the following: <code ; output> IndexError: list index out of range </code> Sooner or later, every Pythonista will write a code that raises an exception, and that is why it is s
- 2.4 Decorators
- message logging; * thread synchronization; * code refactorization; * caching. == Function decora... 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(function): print('We are about to call "{}"'.format(function._
- 4.1 Shallow and deep copy operations
- . It’s hard to imagine writing a piece of Python code that performs any kind of data processing without... pying them. When you spot the following clause: <code python> a_list = [ 1, 'New York', 100] </code> {{ :info:cursos:pue:python-pcpp1:pasted:20231026-143815... treat it as an absolute memory address. Run the code presented in the right pane to see how the string
- 5.1 Metaprogramming
- he ability to modify their own or other programs’ codes. It may sound like an idea from a science fictio... and implemented in the early 1960s. For Python, code modifications can occure while the code is being executed, and you might have already experienced it wh... ws programmers to minimize the number of lines of code to express a solution, in turn reducing developme
- 4.2 Serialization of Python objects using the pickle module
- 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_dict = dict() a_dict['EUR'] = {'code':'Euro', 'symbol': '€'} a_dict['GBP'] = {'code':'
- 2.2 Inheritance and polymorphism — Inheritance as a pillar of OOP
- ple of two-level inheritance is presented here: <code python>class Vehicle: pass class LandVehicle... ass class TrackedVehicle(LandVehicle): pass</code> All the presented classes are empty for now, as... want to call the method info(), which part of the code would be executed then? Python lets you implemen... class hierarchy. Can you guess the output of the code? <code python>class A: def info(self):
- 2.9 Inheriting properties from built-in classes
- the problem and not on type control. Look at the code presented in the editor pane. <code python> class IntegerList(list): @staticmethod def check_... ent failed') print('Final result:', int_list) </code> Something that’s worth commenting on is that we... gated this responsibility to only one method, the code will be shorter, cleaner and easier to maintain.
- 1.2 Working with class and instance data – instance variables
- instance variable is created and accessed in the code presented below. <code python> class Demo: def __init__(self, value): self.instance_var = ... stance variable is equal to:", d2.instance_var) </code> - **%%__init__%%** creates an instance_var var... operty that is present for every Python object. <code python> class Demo: def __init__(self, value)
- 2.5 Different faces of Python methods
- , the instance must have previously existed. The code in the editor demonstrates the idea presented above. <code python> class Example: def __init__(self, val... .get_internal()) print(example2.get_internal()) </code> Each of the Example class objects has its own c... ent word, but you must do it consistently in your code. It follows from the convention that //self// lit
- 2.3 Extended function argument syntax
- e most basic example is a **print()** function: <code python> print() print(3) print(1, 20, 10) print('--', '++') </code> All presented invocations of the **print()** fu... g example can be presented with a list function: <code python> a_list = list() b_list = list(10, 20, 43,... 23, 23, 34, 23, 2) print(a_list) print(b_list) </code> Similarly to the **print()** function, the **lis
- 2.6 Abstract classes
- lasses? The very important reason is: we want our code to be polymorphic, so all subclasses have to deli... lable, you won’t have to rework your music player code, you’ll just have to deliver a class supporting t... t with a typical class that can be instantiated: <code python> class BluePrint: def hello(self): ... nless you need it') bp = BluePrint() bp.hello() </code> There’s nothing new for you here – it’s just an
- 2.1 Python core syntax
- appropriate action. Look at the following simple code: <code python>number = 10 print(number + 20) </code> It is in fact translated to: <code python>number = 10 print(number.__add__(20))</code> Try running the
- 1.1 Classes, Instances, Attributes, Methods — introduction
- sses with a minimum of new syntax and semantics: <code python> class Duck: def __init__(self, height... def quack(self): return print('Quack') </code> In the code above, we have defined a class named Duck, consisting of some functionalities and attribut... es. A class is a place which binds data with the code. If you run the code, there are no visible effec
- 2.7 Encapsulation
- s concept has some interesting features: * the code calling the proxy methods might not realize if it... rstand someone or something (it could be a legacy code) that makes use of your objects. Let's start wit... absence of such access restrictions. So, if your code does intentionally access the attributes marked a... e encapsulated attribute. Let's have look at the code in the editor. <code python> class TankError(Exc
- 2.8 Composition vs Inheritance - two ways to the same destination
- . The primary use of inheritance is to reuse the code. If two classes perform similar tasks, we can cre... during program runtime. Let's try to write some code to see how composition works. Look at the simple code presented in the editor pane. <code python> class Car: def __init__(self, engine): self.en