Polymorphism in Rust and Python. Simple examples
Tags: rust, python, polymorphism, beginners
Published: 2024-02-23 14:13:56

##Disclaimer
The article contains code samples, but not theory and does not pretend to explain the theoretical basis of polymorphism in depth, nor does it explain why and when polymorphism should be used.
The article demonstrates how to implement polymorphism in Rust and Python, but does not explain practical applications of polymorphism in real projects.

##Polymorphism
allows you to write code that can work with different types of objects that share some common behavior or interface. For example, you can write a function that can accept any object that can make a sound, such as a dog, a cat, or a bird.

##In Rust
one way to achieve polymorphism is to use traits and generics. Traits define a set of methods that a type must implement to satisfy a certain interface. Generics allow you to write code that can work with any type that meets some trait bounds. For example, you can write a function that can accept any type that implements the SoundMaker trait, which defines a method called make_sound. Here is an example of polymorphism in Rust using traits and generics:
```Rust
// Define a trait that requires a method called make_sound
trait SoundMaker {
    fn make_sound(&self);
}

// Implement the trait for different types
impl SoundMaker for String {
    fn make_sound(&self) {
        println!("{} says hello", self);
    }
}

impl SoundMaker for i32 {
    fn make_sound(&self) {
        println!("{} beeps", self);
    }
}

// Write a generic function that can accept any type that implements the trait
fn make_some_sound<T: SoundMaker>(item: T) {
    item.make_sound();
}

// Call the function with different types
fn main() {
    let name = String::from("Alice");
    let number = 42;
    make_some_sound(name); // Alice says hello
    make_some_sound(number); // 42 beeps
}
```

##In Python
one way to achieve polymorphism is to use duck typing and inheritance. Duck typing is a concept that says that an object's behavior is more important than its type. In other words, if an object can do something, it doesn't matter what type it is. Inheritance is a concept that allows a class to inherit the attributes and methods of another class. For example, you can write a function that can accept any object that has a method called make_sound. Here is an example of polymorphism in Python using duck typing and inheritance:
```Python
# A class that has a method called make_sound
class SoundMaker:
    def make_sound(self):
        pass

# A subclasses that inherit from the base  class and override the method
class String(SoundMaker):
    def __init__(self, value):
        self.value = value

    def make_sound(self):
        print(f"{self.value} says hello")

class Integer(SoundMaker):
    def __init__(self, value):
        self.value = value

    def make_sound(self):
        print(f"{self.value} beeps")

# A function that can accept any object that has a method called make_sound
def make_some_sound(item):
    item.make_sound()

# Call the function with different objects
name = String("Alice")
number = Integer(42)
make_some_sound(name)  # Alice says hello
make_some_sound(number)  # 42 beeps
```

##Conclusion

As you can see, the examples are similar in the sense that they both use a common interface (make_sound) to achieve polymorphism. However, they also differ in the syntax and the concepts they use. Rust is more explicit and type-safe, while Python is more dynamic and flexible. Both languages have their own advantages and disadvantages, depending on the use case and the preference of the programmer.


![Comparison](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ujpcqp5d05bb2fcew7ig.png)

## Other articles about the similarities and differences between Rust and Python

1. [Python Classes vs. Rust Traits](https://dev.to/antonov_mike/python-classes-vs-rust-traits-a-comparison-of-object-oriented-programming-features-5gbl)
2. [Python classes vs Rust structures](https://dev.to/antonov_mike/python-classes-vs-rust-structures-nod)
3. [Polymorphism in Rust and Python](https://dev.to/antonov_mike/polymorphism-in-rust-and-python-simple-examples-29co)
4. [Abstraction in Rust and Python](https://dev.to/antonov_mike/abstraction-in-rust-and-python-simple-examples-5dd8)
5. [Encapsulation in Rust and Python](https://dev.to/antonov_mike/how-rust-and-python-implement-encapsulation-4ick)
6. [Composition in Rust and Python](https://dev.to/antonov_mike/composition-in-rust-and-python-21c3)