Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara What is Polymorphism? | Polymorphism and Abstraction
In-Depth Python OOP

bookWhat is Polymorphism?

Polymorphism is one of the fundamental concepts in object-oriented programming (OOP). It refers to the ability of different classes to share the same attributes and methods while exhibiting different behaviors.

Let's consider an example of polymorphism:

1234567
dct = {1: "first", 2: "second", 3: "third"} string = "Codefinity" lst = [1, 2, 3, 4, 5] print(len(dct)) # counts the keys in dictionary print(len(string)) # counts the symbols in string print(len(lst)) # counts the elements in list
copy

The len() function exhibits different behaviors depending on the type of data it operates on. This example serves as a demonstration of polymorphism in action.

Polymorphism Implementation

To implement polymorphism, we can create classes that share the same attributes and methods:

123456789101112131415161718192021222324252627
class User: role = "User" def info(self, some_value): print("This is standart user.", some_value + 100) class Admin: role = "Admin" def info(self, some_value): print("This is admin of this service.", some_value * 100) class Hacker: role = "Hacker" def info(self, some_value): print("This person can thief your data.", some_value * 0) user = User() admin = Admin() hacker = Hacker() lst = [user, admin, hacker] for item in lst: print(item.role) item.info(33)
copy

Note

Polymorphism in Python refers to the ability of objects to exhibit different behaviors while performing the same actions.

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 4. Capitolo 1

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Awesome!

Completion rate improved to 2.78

bookWhat is Polymorphism?

Scorri per mostrare il menu

Polymorphism is one of the fundamental concepts in object-oriented programming (OOP). It refers to the ability of different classes to share the same attributes and methods while exhibiting different behaviors.

Let's consider an example of polymorphism:

1234567
dct = {1: "first", 2: "second", 3: "third"} string = "Codefinity" lst = [1, 2, 3, 4, 5] print(len(dct)) # counts the keys in dictionary print(len(string)) # counts the symbols in string print(len(lst)) # counts the elements in list
copy

The len() function exhibits different behaviors depending on the type of data it operates on. This example serves as a demonstration of polymorphism in action.

Polymorphism Implementation

To implement polymorphism, we can create classes that share the same attributes and methods:

123456789101112131415161718192021222324252627
class User: role = "User" def info(self, some_value): print("This is standart user.", some_value + 100) class Admin: role = "Admin" def info(self, some_value): print("This is admin of this service.", some_value * 100) class Hacker: role = "Hacker" def info(self, some_value): print("This person can thief your data.", some_value * 0) user = User() admin = Admin() hacker = Hacker() lst = [user, admin, hacker] for item in lst: print(item.role) item.info(33)
copy

Note

Polymorphism in Python refers to the ability of objects to exhibit different behaviors while performing the same actions.

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 4. Capitolo 1
some-alt