AmitDiwan has Published 11365 Articles

Why are default values shared between objects in Python?

AmitDiwan

AmitDiwan

Updated on 19-Sep-2022 12:24:28

1K+ Views

The default values concept in Python are based on using mutable or immutable objects. It is good programming practice to not use mutable objects as default values. Instead, use None as the default value to avoid issues. Immutable objects such as numbers, strings, tuples, and None, are safe from change. ... Read More

What are the “best practices” for using import in a Python module?

AmitDiwan

AmitDiwan

Updated on 19-Sep-2022 12:19:22

2K+ Views

The import statement, just like any other statement or keyword in Python should be used and added to the code properly following the best practices. Let’s see them one by on − Multiple Imports Multiple Imports should usually be on separate lines. For example − import numpy import pandas import ... Read More

How do I share global variables across modules in Python?

AmitDiwan

AmitDiwan

Updated on 19-Sep-2022 12:18:29

5K+ Views

To share global variable across module in Python, let us first understand what are global variables and its scope. Global Variable ExampleIf a variable is accessible from anywhere i.e. inside and even outside the function, it is called a Global Scope. Let’s see an example − # Variable i = ... Read More

Why do Python lambdas defined in a loop with different values all return the same result?

AmitDiwan

AmitDiwan

Updated on 19-Sep-2022 12:17:36

65 Views

Before understanding why Python lambdas defined in a loop with different values all return the same result, let us first learn about Lambda. Python Lambda The Lambda expressions allow defining anonymous functions. A lambda function is an anonymous function i.e. a function without a name. Let us see the syntax ... Read More

What does the slash(/) in the parameter list of a function mean in Python?

AmitDiwan

AmitDiwan

Updated on 19-Sep-2022 12:03:45

5K+ Views

A slash in the argument list of a function denotes that the parameters prior to it are positional-only. Let us first see a function in Python with a parameter − Function in Python Example Here, we are creating a basic function in Python with a parameter myStr − # Creating ... Read More

Memory Management in Python

AmitDiwan

AmitDiwan

Updated on 19-Sep-2022 11:58:17

859 Views

Writing a memory-efficient and a code that executes quickly is what every developer wants while working on any programming language. In Python, memory allocation and deallocation is not manual, since Python has a Garbage Collector. Now, what is a Garbage Collector. Garbage Collector Garbage Collection is how the memory is ... Read More

How do I modify a string in place in Python?

AmitDiwan

AmitDiwan

Updated on 16-Sep-2022 13:00:42

2K+ Views

Unfortunately, you cannot modify a string in place because strings are immutable. Simply create a new string from the several parts you want to gather it from. Though, if you still need an object with the ability to modify in-place unicode data, you should for The io.StringIO object The ... Read More

Why are floating-point calculations inaccurate in Python?

AmitDiwan

AmitDiwan

Updated on 16-Sep-2022 12:55:08

1K+ Views

What is a float? The floating-point are also called floats, they represent real numbers and are written with a decimal point dividing the integer and fractional parts. Floats may also be in scientific notation, with E or e indicating the power of 10 (2.5e2 = 2.5 x 102 = 250). ... Read More

How can my code discover the name of an object in Python?

AmitDiwan

AmitDiwan

Updated on 16-Sep-2022 12:54:22

1K+ Views

No, there’s no way to discover the name of an object in Python. The reason is that the objects don’t really have names. Let’s say we have the following code. Herein, we cannot find the actual instance name. Since both ob1 and ob2 are bound to the same value, we ... Read More

How do I get a list of all instances of a given class in Python?

AmitDiwan

AmitDiwan

Updated on 16-Sep-2022 12:53:20

5K+ Views

The gc or weakref module is used to get a list of all instances of a given class. First, we will install the gc module using pip − pip install gc To use the gc module, use the import − import gc Get the instances of a class ... Read More

Advertisements