Programming Articles - Page 596 of 3363

How do I share global variables across modules in Python?

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

6K+ 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 = 10 # Function def example(): print(i) print(i) # The same variable accessible outside the function # Calling the example() function example() # The same variable accessible outside print(i) Output 10 10 10 Share Information Across Modules Now, to share information across modules ... Read More

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

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

140 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 − lambda arguments: expressions The keyword lambda defines a lambda function. A lambda expression contains one or more arguments, but it can have only one expression. Example Let us see an example myStr = "Thisisit!" (lambda myStr : print(myStr))(myStr) Output Thisisit! Python lambdas defined in a loop ... Read More

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

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 a Function def demo(myStr): print("Car =: ", myStr) # function call demo("BMW") demo("Tesla") Output Car =: BMW Car =: Tesla Slash in the parameter list of a function As said above, a slash in the argument list of a function denotes that the parameters ... Read More

Memory Management in Python

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

867 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 freed when not use and how it can be made available for other objects. Python deletes the objects that are no longer in use. This is what we call Garbage Collection. The garbage collector initiates its execution with the program and is activated if the reference count drops to zero. ... Read More

How do I modify a string in place in Python?

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 Array module Let’s see what we discussed above − Return a string with the entire contents of the buffer Example In this example, we will return a string with the entire contents of the buffer. We have a text stream StringIO − import io myStr = "Hello, How ... Read More

Why are floating-point calculations inaccurate in Python?

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

2K+ 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). Why floating-point calculations inaccurate? The floating-point calculations are inaccurate because mainly the rationals are approximating that cannot be represented finitely in base 2 and in general they are approximating numbers which may not be representable in finitely many digits in any base. Example Let’s say we have a fraction − ... Read More

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

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

2K+ 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 cannot come to a conclusion that whether the instance name of ob1 or ob2 − Example # Creating a Demo Class class Demo: pass Example = Demo ob1 = Example() ob2 = ob1 print(ob2) print(ob1) Output As we saw above, we ... Read More

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

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

6K+ 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 using the gc module In this example, we have created a Demo class with four instances − ob1 = Demo() ob2 = Demo() ob3 = Demo() ob4 = Demo() We loop through the objects in memory − for ob in gc.get_objects(): Example Using the isinstance(), each object is ... Read More

How do I use strings to call functions/methods in Python?

AmitDiwan
Updated on 16-Sep-2022 12:50:43

7K+ Views

Python Functions are generally called using their name. However, you can also use strings to call functions. For that, use the locals() and globals(). Call functions using strings Example In this example, we will learn how to call two functions using strings − def demo1(): print('Demo Function 1') def demo2(): print('Demo Function 2') locals()['demo1']() globals()['demo2']() Output Demo Function 1 Demo Function 2 Call a function using string variable Example In this example, we have created a class Example with the function xyzuvw() that accepts arg and print them. The globals() ... Read More

How do I specify hexadecimal and octal integers in Python?

AmitDiwan
Updated on 16-Sep-2022 12:49:28

10K+ Views

The Hexadecimal and Octal are part of Numeric Types in Python. Let’s see how to specify them one by one. For hexadecimal type, add a preceding 0x. For example − 0x11 For Octal type (base 8), add a preceding 0 (zero). For example − 0O20 Hexadecimal Integers in Python Hexadecimal Number System uses 10 digits and 6 letters, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F Letters represent the numbers starting from 10. A = 10. B = 11, C = 12, D = 13, E = 14, F = ... Read More

Advertisements