Тёмный

Functional vs Object Oriented Programming in Python 

BeAPythonDev
Подписаться 688
Просмотров 14 тыс.
50% 1

For more leadership and personal growth tips please drop by my other channel: / @stefanbstreet8634
If you found this content helpful please do consider subscribing to the channel, it's a huge help. And let me know what else within your programming journey you'd like to learn about in the comments!
Answering some common questions on functional vs objected oriented in programming and then showing a few code examples to demonstrate a pure function and an object that stores state with a function that modifies the internal data.
Timestamps:
0:00 - Introduction
0:05 - Is Python an object oriented programming language
0:33 - Can you do functional programming in Python?
0:53 - Common object oriented languages used in a company
1:10 - Differences between functional and objected oriented programming
2:15 - How to make good classes in Python
4:05 - Unit testability of functional code compared to classes
4:35 - Writing a functional math module
7:18 - Why functional programming is good for concurrency
8:01 - Using list comprehensions for functional transformations
9:35 - Converting our functional code to a do_math class (should be DoMath)
11:40 - Altering the state of our class data
13:35 - Outro
Object oriented code:
pastebin.com/G2fkLrvL
Article:
beapython.dev/2020/01/21/func...

Опубликовано:

 

5 июн 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 29   
@pcl1923
@pcl1923 11 месяцев назад
One of the best videos explaining this OO vs Functional programming. Concise, with clear examples and a coding demo which is the best way to understand the concepts imo. This deserves more views. Great work.
@RealCaptainAwesome
@RealCaptainAwesome 2 года назад
Cool video. I have zero experience with Python (unless you read my resume) this is a great intro.
@TheFirearmada
@TheFirearmada 3 года назад
Thanks for the video, just getting back in to coding after a LONG break. C++ was what I used to program in, now everything at work is using Python. Fun times learning it.
@BeAPythonDev
@BeAPythonDev 3 года назад
Yeah I learned C++ in College and occasionally do some work in it but Python is just so much more intuitive to me. I prefer to get some good coding done and not spend a lot of time trying to figure out specific syntax to do something. Good luck in getting back into coding. Let me know if there's any other kind of content I can make that you and others would find helpful. Always looking for ideas!
@pwnisherino7414
@pwnisherino7414 2 года назад
Great video, thanks
@bend.4506
@bend.4506 2 года назад
Could you call the functional commands in your class methods? I realize here it would just be extra work. Extract variables from self and input to external function from method. However, let’s say you have a bunch of existing complex functions that you don’t want to have to rewrite as methods. Is it had practice to strip the properties of a class as inputs for a function to prevent having similar methods and classes existing in your code library?
@bend.4506
@bend.4506 2 года назад
Bad*
@romangeneral23
@romangeneral23 3 года назад
I love the war between Object Oriented and Functional. Can't we all just get along!!!
@BeAPythonDev
@BeAPythonDev 3 года назад
Seriously! One of my previous coworkers was so anti OOP. We could never get him to write a class.
@romangeneral23
@romangeneral23 3 года назад
@@BeAPythonDev Side Note: Move your camera head. Blocking the outputs
@arthurswanson3285
@arthurswanson3285 3 года назад
There can be only One.
@DanieleNiero
@DanieleNiero 3 года назад
I don't understand certain programmers. It's like if a carpenter refuses to use anything but a hammer. Why don't use the best tool for the job ahead, like any sane person would do?
@torarinvik4920
@torarinvik4920 2 года назад
@@BeAPythonDev LOL
@JeffersonCanedo
@JeffersonCanedo Год назад
Nice vídeo bro
@BeAPythonDev
@BeAPythonDev Год назад
Thanks for coming by!
@TastyTales792
@TastyTales792 3 года назад
man... this is very much an unresearched video. Why you want to pass functions to functions? -> dependency injection. Also functional programming is about immutable data structures, types, and composition everywhere. We didn't even see a hint of composition, no currying, no map, filter, reduce. We didn't even see the tip of the iceberg. Every time you say "I'm not sure why you'd wanna do this" -> that is an indicator of needing more research. Recommentdation: /watch?v=srQt1NAHYC0
@BeAPythonDev
@BeAPythonDev 3 года назад
You're entitled to your opinion. I didn't realize you expected a complete functional tutorial in 20 minutes when the title is an overview about some of the differences between oop and fp. There isn't much point into going into large detail on map and reduce when the creator of python himself encourages people to use list comprehension instead. What do you mean by "why you want to pass functions into functions? -> dependency injection". Still thank you for watching the video and leaving feedback.
@TastyTales792
@TastyTales792 3 года назад
​@@BeAPythonDev I mean that when you parameterized the action with do_math(action, x, y) you essentially created dependency injection, because now your behavior is not hard coded. If you'd partially apply do_math to take in an action, and then return a function with 2 params x,y then it would be even better, since you could do real dependency injection. def do_math(action): def idk_how_to_do_anonymous(x, y): return action(x,y) return idk_how_to_do_anonymous;
@BeAPythonDev
@BeAPythonDev 3 года назад
@@TastyTales792 thanks for the response and the example. Dependency injection is more about passing in generic objects that can be also be mocked for simple unit-testing. The example from the vid was more used to show how functions can be passed as first class variables in python and can be invoked generically as callback functions. Not saying that is a practical way to do math ;) The target audience for this video is people newer to programming concepts with a goal to invigorate further passion for learning more about either OOP or functional paradigms. You clearly are more of a subject matter expert on pure functional coding than I am as I lean more towards OOP programming personally.
@TastyTales792
@TastyTales792 3 года назад
@@BeAPythonDev yeah, in OOP DI is different, but in FP functions are not different from objects, so you can inject behavior with them, and thus alter it based on use-case (unit test is a use-case, and with partial applied things, you can do exactly the same as with standard DI). Also It's fine if you are lean towards one or another, it's just if you are doing a comparison, I'd expect it more in depth on what can each do, and what are the pros/cons. Not just how it looks (or how it's testable), but what are the core principles behind that and especially why (FP's very core is that types are not classes rather Sets of possible values, and composition is everywhere). For example: OOP is more towards the imperative side, where you tell the computer HOW to do things, and FP is more on the declarative side, and is about WHAT to do, and let the computer decide how. Both has its places, and also sorry, for my outrage, it wasn't obvious from the title that this is aimed at beginners. "Functional vs Object Oriented Programming in Python" - this means to me at least that this should be a comprehensive guide on each, which it is not.
@BeAPythonDev
@BeAPythonDev 3 года назад
@@TastyTales792 no problems at all! I appreciate the discussion and anyone reading through the comments in the next several years will get to learn from your experiences as well :) Making a comprehensive guide on this subject in 14 minutes probably isn't possible (especially as a new content creator). Colleges devote entire classes to each. Sorry if this video didn't meet your expectations. If I do make more detailed functional videos in the future I'll be sure to look more into the differences of DI which I was unaware of. As well as dive deeper into FP before even making the videos which is actually why I haven't made anything about the subject since this one. This vid was based on an article on my blog that was performing well in Google Analytics.
@xpkareem
@xpkareem 3 года назад
So functional language is just an OO language but you don't use objects or classes? Or to put it another way, just use a OO language poorly?
@taragnor
@taragnor 3 года назад
Not poorly. Just differently. Just like OO programing has constraints that are imposed on the programmer, functional programming does as well. The primary goal of FP is to greatly minimize mutable state and create as many pure functions as possible. This actually makes things a lot easier to test and also enables the compiler to do quite a lot of optimizations, which is why functional languages like Haskell can be declarative and still get good performance. Because FP has a lot of mathematical equivalencies, you can give the compiler a ton of extra leeway to optimize things. FP also has a lot of roots in math where you can write code that is mathematically provable to work, because everything is effectively just a big math equation ( a reason why recursion is popular in FP). So an actual functional language like Haskell lets you write recursive definitions and the compiler converts them to non-recursive form for faster performance. Many OO languages have adapted functional conventions, like map / filter /etc. and lambdas (a functional creation) are pretty much a must in modern programming.
@wayhuncho6599
@wayhuncho6599 2 года назад
@@taragnor so u just dont use "class" and objects but only pure functions because it has pros in maths and how it works overall
@taragnor
@taragnor 2 года назад
@@wayhuncho6599 : Well you can still potentially use classes for grouping data, but the main tenet of functional programming is immutability and a lack of side effects. So unlike OOP, your classes don't hold mutable state. That is, a class remains unchanged after you initialize it. Many languages like Java have some immutable classes, like Strings. For instance, when you do something like concatenate a string, you don't modify either strings, you return a new string that's a concatenation of both.
@jesuisravi
@jesuisravi 2 года назад
you speak very well, but you don't magnify the code so the video is very hard to follow. Please magnify.
@kmanccr
@kmanccr Год назад
and your image is over the output
@BeAPythonDev
@BeAPythonDev Год назад
"Thanks for sharing your software development knowledge". Some people will always focus on those minor details when the main takeaway is not the actual output.
Далее
Object Oriented Programming vs Functional Programming
18:55
Every Python dev falls for this (name mangling)
14:11
Просмотров 136 тыс.
I Built 7 EXTREME Rooms in My House!
1:22:07
Просмотров 8 млн
Что за обновление в Tanks Blitz?
1:19:16
Python Decorators in 15 Minutes
15:14
Просмотров 423 тыс.
super/MRO, Python's most misunderstood feature.
21:07
Просмотров 211 тыс.
QUESTIONABLE Object Creation Patterns in Python 🤔
15:10