Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Redundancy In Python?
#1
I was watching this video, "How the C++ Compiler Works"...

The author showed that how this...
int Multiply(int a, int b)
{
    int result = a * b;
    return result;
}
Became redundant in the ASM output...
	mov	eax, DWORD PTR a$[rsp]
	imul	eax, DWORD PTR b$[rsp]
	mov	DWORD PTR result$[rsp], eax
	mov	eax, DWORD PTR result$[rsp]

int multiply(const int a, const int b) {
    return a * b;
}
	mov	eax, DWORD PTR a$[rsp]
	imul	eax, DWORD PTR b$[rsp]

This makes me wander about a standard practice that I've picked up about readability in Python code...
completed_width = int((current_items * width) / total_items)
remaining_width = width - completed_width
progress = f"[white]{'─' * completed_width}[/][gray]{'─' * remaining_width}[/]"
console.update(progress)
I can somewhat understand this. This is like following the English language as if you were writing a book, over using slang written in English, but...

I'm wondering how much of this would become redundant after it has been compiled into bytecode. This also makes me wonder what Python's bytecode would look like after translating it to ASM.

How redundant can your code become in python? Does anyone have any input on this?
Reply
#2
You can visualize Python bytecode with the dis module
>>> import dis
>>> 
>>> def multiply(a, b):
...     return a * b
... 
>>> def multiply2(a, b):
...     result = a * b
...     return result
... 
>>> dis.dis(multiply)
  1           0 RESUME                   0

  2           2 LOAD_FAST                0 (a)
              4 LOAD_FAST                1 (b)
              6 BINARY_OP                5 (*)
             10 RETURN_VALUE
>>> dis.dis(multiply2)
  1           0 RESUME                   0

  2           2 LOAD_FAST                0 (a)
              4 LOAD_FAST                1 (b)
              6 BINARY_OP                5 (*)
             10 STORE_FAST               2 (result)

  3          12 LOAD_FAST                2 (result)
             14 RETURN_VALUE
>>> 
Python does not translate to assembly language however. But some modules translate Python to C code which you could translate to asm.
phpjunkie likes this post
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
Great question! Redundancy in Python generally refers to having repeated or unnecessary code, which can lead to inefficiencies or harder-to-maintain code. It's important to identify and remove redundancy to keep the code clean and readable.

Here are a few common examples of redundancy in Python:

Repetitive Code Blocks: If you're writing the same code in multiple places, consider creating functions to avoid repetition.

Redundant code:

result = 5 * 2 + 3
print(result)

result = 7 * 3 + 4
print(result)

Refactored code:

def calculate_and_print(x, y, z):
result = x * y + z
print(result)

calculate_and_print(5, 2, 3)
calculate_and_print(7, 3, 4)

Using Unnecessary Variables: Sometimes you create variables that aren't necessary, which can make the code more complex than it needs to be.

Redundant code:

temp = 5
result = temp + 10
print(result)

Refactored code:

result = 5 + 10
print(result)

Redundant Imports: Avoid importing the same library multiple times in the same file or module.

Redundant code:

import math
import math # Duplicate import

Refactored code:

import math # Only import once
To avoid redundancy, a good rule of thumb is:

DRY ("Don’t Repeat Yourself"): Try to minimize repetition of logic or code patterns, using functions, loops, or classes where necessary.

Hope that clears up the concept of redundancy in Python! Let me know if you'd like more examples or further explanation.
Hi there! I’m a Python enthusiast passionate about building projects and solving coding challenges. Currently, I’m exploring ways to integrate Python with new tools and platforms. One of the interesting tools I've come across recently is the link removed, which helps with streamlined media browsing and management. If anyone has experience using it with Python, feel free to share your insights!
Reply
#4
(Feb-17-2026, 08:40 AM)Gribouillis Wrote: You can visualize Python bytecode with the dis module
>>> import dis
>>> 
>>> def multiply(a, b):
...     return a * b
... 
>>> def multiply2(a, b):
...     result = a * b
...     return result
... 
>>> dis.dis(multiply)
  1           0 RESUME                   0

  2           2 LOAD_FAST                0 (a)
              4 LOAD_FAST                1 (b)
              6 BINARY_OP                5 (*)
             10 RETURN_VALUE
>>> dis.dis(multiply2)
  1           0 RESUME                   0

  2           2 LOAD_FAST                0 (a)
              4 LOAD_FAST                1 (b)
              6 BINARY_OP                5 (*)
             10 STORE_FAST               2 (result)

  3          12 LOAD_FAST                2 (result)
             14 RETURN_VALUE
>>> 
Python does not translate to assembly language however. But some modules translate Python to C code which you could translate to asm.
This is a guess, but I think when you compile a C/C++ program it is translated into assembly then it is run through an assembler to turn it into machine code but we are talking about Python and I think Python acts as a Virtual Machine Interpreter where the Virtual Machine itself is the machine code and the byte code from Python never touches the CPU.

Please correct me if I'm wrong.
Reply
#5
(May-27-2026, 09:31 PM)phpjunkie Wrote: Python acts as a Virtual Machine Interpreter where the Virtual Machine itself is the machine code and the byte code from Python never touches the CPU.
This is correct. Python's byte code is not the same as assembly language. It cannot be translated to asm because it manipulates high level data structures such as Python integers, lists, dictionaries etc. For example adding two Python integers has a very different effect from adding two long integers in C because Python integers can have an arbitrary bit length.

However, Python byte code is an intermediary step between your code and the machine, so it makes sense to track redundancies in byte code too.
« We can solve any problem by introducing an extra level of indirection »
Reply
#6
Some programming languages run inside a virtual machine (VM) or managed runtime environment.

Programming languages can also be categorized by their type systems, such as statically typed and dynamically typed languages.
  • Java and C# are statically typed languages, and both typically run on a virtual machine or managed runtime:
    • Java runs on the Java Virtual Machine (JVM).
    • C# runs on the Common Language Runtime (CLR) in the .NET platform.
  • Python and JavaScript are dynamically typed languages:
    • Python is usually executed by the CPython interpreter, which compiles source code into bytecode that runs on the Python virtual machine.
    • JavaScript engines such as V8 or SpiderMonkey also use internal virtual machines and just-in-time (JIT) compilation techniques.

Many modern languages first compile source code into an intermediate representation, often called bytecode or intermediate language (IL), which is then executed by a virtual machine. This approach improves portability and enables runtime optimizations such as JIT compilation.

In Python, source code is automatically compiled into bytecode before execution. During this process, CPython performs a few basic optimizations, such as constant folding and removal of unreachable assertions in optimized mode, but these optimizations are relatively limited compared to those performed by advanced JIT compilers.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020