-
Python - Context Managers
摘要:with statement Here is the syntax of the with statement: with expression as var: statements The expression should be a context manager object, or it s
阅读全文
-
Python - Built-in Exceptions: Python Exceptions Class Hierarchy
摘要:Figure 20.4: Built-in exceptions The class BaseException is the base class of all the built-in exception classes. From BaseException, four classes nam
阅读全文
-
Python - Strategies to handle exceptions in your code
摘要:There are two approaches that can be followed when we want to deal with exceptions that occur due to unusual events: LBYL - Look Before You Leap EAFP
阅读全文
-
Python - operator module
摘要:>>> list(map(lambda x, y: x * y, [1,2,3,4], [5,6,7,8])) [5, 12, 21, 32] Here we have called the map function and sent a lambda expression as first arg
阅读全文
-
Python - Functional programming
摘要:Functional programming is a programming paradigm in which most of the work in a program is done using pure functions. A pure function is a function wi
阅读全文
-
Python - Creating jump tables using lambda functions
摘要:We can place lambda function inside list and dictionary literals. This way we can use lambda expressions to create jump tables. >>> L = [lambda s: s.s
阅读全文
-
Python - Lambda expressions as closures
摘要:A closure is a nested function that can access free variables from an enclosing function even after it has finished its execution. We know that, like
阅读全文
-
Python - Class Decorators
摘要:We have used functions to decorate functions and to decorate classes. Now, we will see how to define a class as a decorator. At the start of this chap
阅读全文
-
Python - Decorating classes
摘要:When we talk about decorating classes, we can either decorate individual methods or we can create a decorator to decorate the whole class. class MyCla
阅读全文
-
Python - Decorators
摘要:A decorator is a callable that takes a callable as input and returns a callable. This is the general definition of a decorator. The callable in this d
阅读全文
-
Python - Generators
摘要:The task of implementing iterators can be simplified by using generators. We have seen how to create custom iterators using the object-oriented way, i
阅读全文
-
Python - itertools Module
摘要:The itertools module contains special functions to create iterators for efficient looping. It has three broad categories of iterators - infinite itera
阅读全文
-
Python - Making your class Iterable
摘要:If we want objects of this class to be used in iteration contexts, then they should support the iteration protocol. To make this class iterable, we ca
阅读全文
-
Python - Creating your own Iterator
摘要:In our first example, we will create iterable objects, which, when iterated over, will give out cubes of numbers, and these objects will support multi
阅读全文
-
Python - Iterator vs Iterable
摘要:There are many built-in functions and methods that return iterables and iterators. Here are a few examples: range() returns an iterable dict.keys() re
阅读全文
-
Python - Composition
摘要:class Engine: def __init__(self,power): self.power = power def start(self): self.draw_current() self.spin() self.ignite() def draw_current(self): prin
阅读全文
-
Python - Abstract Base classes
摘要:We have seen that if we have to define a group of classes that have similar features and show common behavior, we can define a base class and then inh
阅读全文
-
Python - Polymorphism
摘要:The three main features of object-oriented programming are - encapsulation, inheritance and polymorphism. We have seen the first two, now let us see w
阅读全文
-
Python - Method Resolution Order (MRO)
摘要:The order in which Python searches for attributes in base classes is called method resolution order(MRO). It gives a linearized path for an inheritanc
阅读全文
-
Python - String representation of an instance object
摘要:The magic methods __str__ and __repr__ are used for converting an instance object into a string. The method __str__ is invoked when an instance object
阅读全文
-
Python - Properties
摘要:Creating Managed Attributes using properties Properties can be used to create data attributes with special functionality. If you want some extra funct
阅读全文
-
Python - Static Methods
摘要:Sometimes we have to write methods that are related to the class but do not need any access to instance or class data for performing their work. These
阅读全文
-
Python - Creating alternative initializers using class Methods
摘要:Class methods allow us to define alternative initializers (also known as factory methods) in a class. These methods help us create instance objects fr
阅读全文
-
Python - Change current working directory
摘要:os.getcwd() Returns the path of the current working directory os.chdir(path) Changes our current working directory
阅读全文
-
Python - Redirecting output of print to a file
摘要:The print function can also be used to write to a file. The output of print, that is by default, sent to the screen can be redirected to an open file.
阅读全文
-
Python - File opening modes and buffering
摘要:'r' - read mode(default) 'w' - write mode 'a' - append mode 'x' - exclusive creation We know that the mode 'r' opens an existing file for reading only
阅读全文
-
Python - Reloading a module
摘要:Each module is loaded into memory only once during an interpreter session or during a program run, regardless of the number of times it is imported in
阅读全文
-
Python - Function Annotations
摘要:def func(s:str, i:int, j:int) -> str: return s[i:j] The parameter s is supposed to be a string, so we place a colon after the parameter name and then
阅读全文
-
Python - Arguments and Parameters
摘要:Parameters in Function Definition A. def func(name): Match by position or by name B. def func(name=value): Default argument C. def func(*args): Collec
阅读全文
-
Python - Values returned by "and", "or" and "not" operators
摘要:Unlike some other languages, in Python, the logical operators "and" and "or" do not return Boolean values True or False; they actually return the last
阅读全文
-
Python - Using a list with functions from the random module
摘要:To select a random item from the list or shuffle the list, you can use the choice and shuffle functions from the random module of the standard library
阅读全文
-
LibreOffice Writer - How to insert a check/tick mark and circled numbers
|