Pythonic Dev
678 subscribers
103 photos
1 video
25 links
Happy Coding πŸ’«
ADMIN: @cmatrix1
Download Telegram
Pythonic Dev
Mutating an object means changing the object's state without creating a new object @Cmatrix_PY
A type is mutable if you can change the contents of its objects without changing its identity and its type.

@Cmatrix_PY
In Python, everything is an object, and each object is characterised by three things:


1.its identity (an integer that uniquely identifies the object, much like social security numbers identify people);

2.a type (that identifies the operations you can do with your object); and

3.the object's content.

@Cmatrix_PY
A very good indicator that an object is immutable is when all its methods return something

@Cmatrix_PY
Pythonic Dev
#quiz #easy
The defaults arguments are created and stored in special place


@Cmatrix_PY
Pythonic Dev
#quiz #easy
This is why, in general, mutable objects shouldn't be used as default arguments.
This is the standard practice

@Cmatrix_PY
πŸ‘2
Python uses a pass-by-assignment model, and understanding it requires you to realise all objects are characterised by an identity number, their type, and their contents.

@Cmatrix_PY
Pass by value, pass by reference, and pass by assignment are all ways in which arguments can be passed to a function or method.

In pass by value, the actual value of an argument is passed to the function. This means that any changes made to the parameter inside the function have no effect on the original argument. In other words, a copy of the value is sent to the function, so any modifications made within the function will not affect the original variable.

In pass by reference, a reference or pointer to the memory location of the argument is passed to the function. This means that any changes made to the parameter inside the function will also affect the original argument. In other words, both variables point to the same memory address, so any modifications made within the function will also be reflected outside the function.

#python
#pass_by_value
#pass_by_reference
@Cmatrix_PY
πŸ‘2
In pass by assignment, a copy of the reference to the object is created and passed to the function. This means that any changes made to the parameter inside the function may or may not affect the original object depending on the programming language and the type of object being passed. For example, in Python, all objects are passed by assignment, which means that if you pass a mutable object like a list or dictionary to a function and modify it inside the function, those changes will be reflected outside the function as well. However, if you pass an immutable object like a number or string to a function and modify it inside the function, those changes will not be reflected outside the function because a new object is created when the value is changed.

#python
#pass_by_assignment
@Cmatrix_PY
πŸ”₯1
Generally, tuples are more efficient that lists, so, unless you need mutability of the container, prefer using a tuple over a list.

#python
@Cmatrix_PY
Constant folding is the process of recognizing and evaluating constant expressions at compile time rather than computing them at runtime.

In this example, we define a function called calculate() that multiplies the constants 10 and 20 together. Since this expression involves only constants, the Python interpreter can simplify it to 200 during compilation. Therefore, at runtime, the function simply returns the constant value 200, rather than performing the multiplication operation again.

This is a simple example, but constant folding can be very useful for optimizing performance in more complex programs. By simplifying expressions involving only constants, the compiler or interpreter can reduce the amount of work that needs to be done at runtime, leading to faster and more efficient code.

#python
@Cmatrix_PY
Tag in Commits indicates the type of change made.

#git
πŸ‘1
What is a dynamic array?

A dynamic array is similar to an array, but with the difference that its size can be dynamically modified at runtime. Don’t need to specify how much large an array beforehand. The elements of an array occupy a contiguous block of memory, and once created, its size cannot be changed. A dynamic array can, once the array is filled, allocate a bigger chunk of memory, copy the contents from the original array to this new space, and continue to fill the available slots.

#data_structure
@Cmatrix_PY
πŸ‘1
we can use an underscore _ before the method name to keep it non-public.

#python
@Cmatrix_PY
πŸ‘2
Dynamic Array Logic Implementation:

The key is to provide means to grows an array A that stores the elements of a list. We can’t actually grow the array, its capacity is fixed. If an element is appended to a list at a time, when the underlying array is full, we need to perform following steps.


1.Allocate a new array B with larger capacity (A commonly used rule for the new array is to have twice the capacity of the existing array )

2.Set B[i]=A[i], for i=0 to n-1 where n denotes the current no of items.

3.Set A=B that is, we hence forth use B as the array of supporting list.

4.Insert new element in the new array.


#data_structure
@Cmatrix_PY
πŸ‘1
Why tuple Load faster than list?


Tuples can load faster than lists when they contain constants (such as integers and strings) because tuples are immutable, meaning their values cannot be changed once they are created. This immutability allows the interpreter to make certain optimizations during the loading process.

When a tuple is created with constant values, the compiler or interpreter can treat it as a single constant value in memory. This means that the entire tuple can be loaded in one step without any intermediate steps or memory allocations. Since tuples are immutable, there is no need for additional memory allocation or resizing because the values cannot be modified later.

On the other hand, lists are mutable, so they need to support dynamic resizing and element addition. When building a list, each element is added individually, which involves multiple steps of memory allocation, resizing, and assignment. The list needs to allocate memory for each new element, potentially resize the underlying data structure, and copy the existing elements to the new memory location.

Because of these differences, tuples can load faster than lists when they contain constant values because the loading process for tuples is more streamlined and doesn't require the same level of memory management and resizing operations performed by lists.

It's important to note that this performance difference between tuples and lists is generally only noticeable in specific scenarios where large collections or intensive operations are involved. For most general use cases, the speed difference between tuples and lists is unlikely to have a significant impact on overall performance.

#data_structure
#python
@Cmatrix_PY
πŸ‘2πŸ”₯1
πŸ“’ New Article Alert: Test Driven Development of a Django RESTful API ! πŸπŸ”¬

πŸ”— Article Link: https://realpython.com/test-driven-development-of-a-django-restful-api/

In this article, you'll learn about test-driven development (TDD) using Django REST Framework. Discover how to build robust APIs with comprehensive test suites. The article covers setting up your project, writing unit tests for views and serializers and ...

Don't miss out on this opportunity to level up your API testing skills!

Happy coding and testing! πŸ’»πŸ§ͺ

#Django
#API
#restframework
@Cmatrix_PY
❀3