Files

170 lines
13 KiB
Plaintext
Raw Permalink Normal View History

2023-06-14 02:46:49 -05:00
overloading in python:
Function overloading refers to the ability to define multiple functions with the same name but different parameter lists.
However, in Python, you can achieve similar functionality by using default arguments and optional parameters. Here's an example:
def my_function(param1, param2=None):
if param2 is None:
# Perform action when only one parameter is provided
print("Parameter 1:", param1)
else:
# Perform action when both parameters are provided
print("Parameter 1:", param1)
print("Parameter 2:", param2)
In this example, the my_function function takes two parameters. The second parameter param2 is set to None by default, making it optional. Inside the function, you can check if param2 is None to determine whether it was provided or not.
superclass’s __new__() method:
The __new__() method in Python is a special method defined in a class that is responsible for creating and returning a new instance of that class. It is a static method that gets called before the __init__() method and is primarily used in the creation of immutable objects or when you need more control over the object creation process.
When dealing with inheritance, the __new__() method of the superclass is automatically called when creating an instance of a subclass. This happens before the subclass's __init__() method is invoked. The __new__() method of the superclass can be overridden in the subclass to customize the object creation process.
metaclasses:
Metaclasses in Python provide a way to define the behavior and structure of classes themselves. A metaclass is the class of a class, i.e., a class that defines how a class should be created, just as a class defines how an instance should be created. Metaclasses allow you to customize class creation and modify class attributes, methods, and behavior.
class constructor expression:
In Python, the class constructor is defined using the __init__() method. It is called automatically when an object is created from a class and is responsible for initializing the object's attributes.
class MyClass:
def __init__(self, param1, param2):
self.param1 = param1
self.param2 = param2
# Creating an instance of MyClass
obj = MyClass("Hello", 42)
gc module:
The gc module in Python provides an interface to the garbage collector and related functions for managing memory and collecting unused objects. It is part of Python's memory management system and allows you to control and monitor the garbage collection process.
“rich comparison” methods:
"Rich comparison" methods, also known as "comparison operators" or "comparison magic methods," are special methods in Python that allow objects to define their own behavior for comparison operations such as equality, inequality, less than, greater than, etc. These methods enable you to customize how objects are compared and ordered.
By implementing rich comparison methods in your classes, you can define custom rules for comparison operations and make objects behave in a way that suits your needs.
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
def __eq__(self, other):
if isinstance(other, Rectangle):
return self.width == other.width and self.height == other.height
return False
def __lt__(self, other):
if isinstance(other, Rectangle):
return self.width * self.height < other.width * other.height
return NotImplemented
rect1 = Rectangle(3, 4)
rect2 = Rectangle(4, 5)
rect3 = Rectangle(3, 4)
print(rect1 == rect2) # Output: False
print(rect1 == rect3) # Output: True
print(rect1 < rect2) # Output: True
swapped-argument versions:
The "swapped-argument versions" of the rich comparison methods refer to the reversed version of the original comparison methods. These methods allow you to define the behavior when the operands of the comparison are swapped.
hashed collections:
Hashed collections in Python are data structures that use hashing algorithms to provide efficient lookup and retrieval of elements. They are designed to offer fast access to elements based on their unique hash values.
There are several hashed collection types available in Python's standard library:
dict (dictionary): A dictionary is an unordered collection of key-value pairs. It uses a hash table implementation, where the keys are hashed to provide fast lookup and retrieval of values. Dictionaries are commonly used when you need to map unique keys to corresponding values.
set: A set is an unordered collection of unique elements. It uses hash-based lookup to quickly determine membership of an element. Sets are useful when you want to store a collection of items without any duplicates and perform set operations such as union, intersection, and difference efficiently.
frozenset: Similar to a set, a frozenset is an immutable version of a set. Once created, its elements cannot be modified. Frozensets are often used as keys in dictionaries or elements in other sets because they are hashable.
salted in python:
In Python, salting is commonly used in password hashing and encryption algorithms. Here's an example of how salting can be implemented using the bcrypt library, which is a popular choice for secure password hashing:
import bcrypt
password = "my_password"
salt = bcrypt.gensalt()
# Hash the password with the generated salt
hashed_password = bcrypt.hashpw(password.encode(), salt)
# Verify the password
if bcrypt.checkpw(password.encode(), hashed_password):
print("Password is correct.")
else:
print("Password is incorrect.")
password hashing and encryption algorithms:
Password hashing and encryption algorithms are used to protect sensitive information like passwords by transforming them into a non-readable format. Here are some commonly used algorithms in Python:
bcrypt: bcrypt is a popular password hashing algorithm that incorporates a salt value and multiple iterations of hashing to increase the security of passwords. It is designed to be slow and computationally expensive, making it resistant to brute-force attacks. The bcrypt library in Python provides an easy-to-use interface for working with bcrypt.
Argon2: Argon2 is a state-of-the-art password hashing algorithm that won the Password Hashing Competition in 2015. It is designed to be memory-hard and resistant to various types of attacks, including GPU and ASIC attacks. The argon2 library in Python provides support for Argon2.
PBKDF2: PBKDF2 (Password-Based Key Derivation Function 2) is a key derivation function that applies a cryptographic hash function (such as SHA-1 or SHA-256) iteratively with a salt value to produce a derived key. It is widely used for password hashing. The hashlib library in Python provides support for various hash functions that can be used with PBKDF2.
scrypt: scrypt is a password-based key derivation function that is designed to be memory-hard and resistant to various types of attacks, including hardware-based attacks. It is commonly used for password storage. The scrypt library in Python provides support for scrypt.
AES: AES (Advanced Encryption Standard) is a symmetric encryption algorithm that is widely used for encrypting data. It is a block cipher with various key sizes (128-bit, 192-bit, or 256-bit) and encryption modes (such as ECB, CBC, or GCM). The cryptography library in Python provides support for AES encryption.
O(n2) complexity:
O(n^2) complexity, also known as quadratic complexity, refers to an algorithm whose execution time grows quadratically with the input size. It means that the time required to solve a problem double as the input size increases by a constant factor.
dict insertion:
Dictionary insertion in Python has an average time complexity of O(1) and a worst-case time complexity of O(n) in scenarios where collisions occur frequently.
intentional asymmetry:
Intentional asymmetry, in the context of cryptography, refers to the deliberate introduction of imbalance or non-uniformity in cryptographic algorithms or protocols. It is done to enhance security by making it difficult for attackers to exploit patterns or symmetries that could potentially weaken the system.
cryptography:
Cryptography is the practice of securing communication and data by transforming it into a form that is unintelligible to unauthorized individuals. It involves the use of cryptographic algorithms and protocols to ensure confidentiality, integrity, authentication, and non-repudiation of information.
runtime introspection of dynamic class attributes:
In Python, you can perform runtime introspection of dynamic class attributes using various built-in functions and attributes. Here are a few techniques:
dir() function: The dir() function returns a list of names in the current local or global scope. When called on a class or an instance, it returns a list of all attributes, including both static and dynamic attributes.
vars() function: The vars() function returns the __dict__ attribute of an object, which contains the object's attributes and their values as a dictionary. When called on a class, it returns the class's namespace dictionary.
getattr() function: The getattr() function allows you to dynamically access an attribute of an object by name. It takes two arguments: the object and the attribute name as a string. If the attribute exists, it is returned; otherwise, an optional default value can be provided.
Multiple inheritance:
Multiple inheritance is a feature in object-oriented programming languages that allows a class to inherit attributes and behaviors from multiple parent classes. In Python, you can create a class that inherits from multiple parent classes by listing them in the class definition separated by commas.
MRO:
MRO stands for Method Resolution Order and refers to the order in which methods are resolved or looked up in a class hierarchy that involves multiple inheritance. When a class inherits from multiple parent classes, the MRO determines the sequence in which the methods are searched for and executed.
lexical scoping:
Lexical scoping, also known as static scoping, is a scoping mechanism used in programming languages to determine the scope of variables and identifiers based on their location in the source code. In lexical scoping, the scope of a variable is determined by its position or context in the program's text or source code, rather than by runtime conditions.
divmod():
result = divmod(17, 5)
print(result) # Output: (3, 2)
pow():
result = pow(2, 3)
print(result) # Output: 8
^:
result = 5 ^ 3
print(result) # Output: 6
In this example, 5 ^ 3 performs a bitwise XOR operation on the numbers 5 and 3. The binary representation of 5 is 101, and the binary representation of 3 is 011. Performing the XOR operation on corresponding bits gives 110, which is equal to 6 in decimal.
&:
result = 12 & 7
print(result) # Output: 4
In this example, 12 & 7 performs a bitwise AND operation on the numbers 12 and 7. The binary representation of 12 is 1100, and the binary representation of 7 is 0111. Performing the AND operation on corresponding bits gives 0100, which is equal to 4 in decimal.
@:
In Python, the @ symbol is used as the matrix multiplication operator. It is also known as the "at" operator or "matmul" operator. The @ operator performs matrix multiplication between two arrays or matrices.
%:
result = 10 % 3
print(result) # Output: 1
<<:
result = 10 << 2
print(result) # Output: 40
In this example, 10 << 2 shifts the bits of the number 10 two positions to the left. The binary representation of 10 is 1010, and shifting it two positions to the left gives 101000, which is equal to 40 in decimal.
|:
result = 5 | 3
print(result) # Output: 7
In this example, 5 | 3 performs a bitwise OR operation on the numbers 5 and 3. The binary representation of 5 is 101, and the binary representation of 3 is 011. Performing the OR operation on corresponding bits gives 111, which is equal to 7 in decimal.
questions:
1.What is the purpose of using a constructor in a class?
2.How do you define a class method in Python?
3.What is the difference between an instance variable and a class variable?
4.How can you access an instance variable within a class method?
5.Can a class method modify the value of a class variable? Why or why not?
6.Explain the concept of method overloading with an example.
7.What is the difference between a shallow copy and a deep copy of an object?
8.How do you define a static method in Python? What is its significance?
9."What is the difference between mutable sequences and immutable sequences in programming, and when would you choose to use one over the other?"
10.Consider a scenario where you have a parent class called Animal and two child classes called Cat and Dog. If you have a variable named animal which is of type Animal, how would you determine if it is an instance of the Cat class using logical deduction instead of direct comparison?