Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do OO design principles apply to Python?

Tags:

python

oop

It seems like many OO discussions use Java or C# as examples (e.g. Head First Design Patterns).

Do these patterns apply equally to Python? Or if I follow the design patterns, will I just end up writing Java in Python (which apparently is a very bad thing)?

like image 813
Jim Avatar asked Feb 13 '09 16:02

Jim


People also ask

Do SOLID principles apply to Python?

Usually, the SOLID principles are applied in the context of object-oriented design (i.e.: Python's classes), but I believe they are valid regardless of the level, and I would like to keep the example and explanation here, to a level for an “advanced beginner”, overseeing formal definition.

Is Python object-oriented design?

Python, like every other object-oriented language, allows you to define classes to create objects. In-built Python classes are the most common data types in Python, such as strings, lists, dictionaries, and so on.

Does Python need design patterns?

Some design patterns are built into Python, so we use them even without knowing. Other patterns are not needed due of the nature of the language. For example, Factory is a structural Python design pattern aimed at creating new objects, hiding the instantiation logic from the user.

Is Python object-oriented or functional?

Python is considered as an object-oriented programming language rather than a procedural programming language.

What is object oriented programming in Python?

The key takeaway is that objects are at the center of object-oriented programming in Python, not only representing the data, as in procedural programming, but in the overall structure of the program as well.

What is the difficulty of Python design patterns?

Python Design Patterns Difficulty Level : Medium Last Updated : 27 Feb, 2020 Design Patterns is the most essential part of Software Engineering, as they provide the general repeatable solution to a commonly occurring problem in software design.

Who created Each tutorial at Real Python?

Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are: What Do You Think?

What is the use of dot operator in Python?

- The reference to self.name tells Python to use the property of the object called name. It first gets a reference to the object (which is self ), then starts looking for a property of the object (which is the dot operator) with the name given (which is name ).


2 Answers

The biggest differences are that Python is duck typed, meaning that you won't need to plan out class hierarchies in as much detail as in Java, and has first class functions. The strategy pattern, for example, becomes much simpler and more obvious when you can just pass a function in, rather than having to make interfaces, etc. just to simulate higher order functions. More generally, Python has syntactic sugar for a lot of common design patterns, such as the iterator and the aforementioned strategy. It might be useful to understand these patterns (I've read Head First and found it pretty useful), but think about Pythonic ways to implement them rather than just doing things the same way you would in Java.

like image 161
dsimcha Avatar answered Oct 20 '22 22:10

dsimcha


Python has it's own design idioms. Some of the standard patterns apply, others don't. Something like strategy or factories have in-language support that make them transparent.

For instance, with first-class types anything can be a factory. There's no need for a factory type, you can use the class directly to construct any object you want.

Basically, Python has its own design idioms that are somewhat different largely because it's so dynamic and has incredible introspection capabilities.

Example:

x = list my_list = x(range(0,5)) #creates a new list by invoking list's constructor 

By assigning the class-type to a callable object you can essentially remove any 'factory' types in your code. You are only left with callables that produce objects that should conform to some given conventions.

Furthermore, there are design patterns in Python that just can't be represented in other statically-typed languages efficiently. Metaclasses and function decorators are good examples of this.

like image 24
user21714 Avatar answered Oct 20 '22 22:10

user21714