You Can Do Really Cool Things With Functions In Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 ธ.ค. 2024

ความคิดเห็น • 378

  • @ArjanCodes
    @ArjanCodes  ปีที่แล้ว +1

    💡 Get my FREE 7-step guide to help you consistently design great software: arjancodes.com/designguide.

  • @davia.sampaio8633
    @davia.sampaio8633 2 ปีที่แล้ว +134

    It's really cool to watch your videos, because they open our minds to other possibilities of how to build the same code. You teach things that are not easy to find anywhere else.

  • @fschlz
    @fschlz 2 ปีที่แล้ว +42

    You sir, are putting out the best Python content on the internet. Thank you!

  • @alira7296
    @alira7296 2 ปีที่แล้ว +50

    Little Vim tip: full stop (.) repeats your last action. This includes entering insert mode and typing things. e.g. at 6:10 you type out "_avg" twice. You could instead just type it once, navigate to the next function, then press . and that'll type out "_avg" again for you. Same with _minmax a few seconds later!
    Edit: Just watched the part about partial functions. Can't believe I've never seen them before, they seem like such a useful tool!

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว +13

      You’re absolutely right. I still have a lot to unlearn from not editing with Vim for about 35 years.

    • @astronemir
      @astronemir 2 ปีที่แล้ว +1

      Wow. I really need to learn Vim properly and use it.

    • @joeymea
      @joeymea 2 ปีที่แล้ว +4

      But also in vscode you can edit hundreds of lines at once (and I have done so in a productive way before)... I take people who talk about vim/emacs being best in the same way I take people talking up the King James Version. Sure, it's the Bible, but the language is archaic and outdated.
      Vim is great, but so is vscode. And this is coming from someone who is efficient in both. I definitely prefer vscode.

    • @lawrencedoliveiro9104
      @lawrencedoliveiro9104 2 ปีที่แล้ว +1

      People think Emacs is old, but they forget that it was built by a whole bunch of smart hackers who lived and breathed coding, knew what they were doing, and were not beholden to marketing managers at any company. For example, they used an advanced programming language (LISP), which can still scare the pants off the Java/C♯/PHP crowd today. Does the automation language in _your_ favourite editor understand closures and lexical binding? (Interestingly, the sort of concepts discussed in this very video.)

    • @ArpadHorvathSzfvar
      @ArpadHorvathSzfvar 2 ปีที่แล้ว

      I like Vim too but it's not good at bigger refactors. So I use PyCharm for bigger projects, and sometimes I just switch on the Vim plugin to do some stuffs. For smaller things (indenting the whole file, removing indentation, adding comma to the and of lines, join all the lines, removing every fifth lines with macro) I just start a Vim in command line.

  • @raeganb.7254
    @raeganb.7254 2 ปีที่แล้ว +16

    This came in handy for my weekend project! I’ve watched through all your Python videos so far, and my code has started to improve dramatically as a result. Thanks for sharing!

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว +2

      You’re welcome - glad to hear that the videos help you improve your code.

  • @MisterMobius
    @MisterMobius 2 ปีที่แล้ว +7

    wow. I am currently working on a project where I use functions that return customized functions, just as in your example. So partial just blew my mind a little. Thank you so much for introducing this awesome tool to me :)

  • @ExplicableCashew
    @ExplicableCashew 2 ปีที่แล้ว +27

    I guess it matters how you define the function of the bread. I may be partial to German bread and will happily eat it by itself, but there's an argument to be made that, say, certain French breads shine as part of a breakfast. With American bread, islice it and toast it and it slaps. Italian bread is in a class of its own. I haven't tried Dutch bread, I gotta find some, I wonder if they import it here. And to give y'all some closure, I'm sure any bread from any country can be great if combined with the right ingredients

    • @theMuritz
      @theMuritz 2 ปีที่แล้ว

      Dutch bread, it’s simply a slice of very soft very limp toast called Roti … maybe that’s why a Dutch can’t understand other countries being fond of their bread … I’ve been living in Indonesia and they had adopted Dutch bread-making habits, while Vietnam for example offer baguette due to the French, and none of them compares to German variety of course …. 😜

  • @robertbrummayer4908
    @robertbrummayer4908 2 ปีที่แล้ว +15

    Great video, Arjan. I also use the functools a lot, e.g. partial. What I really love about Python is that it supports many different approaches and does not force you to use a single one. As a developer I can choose between many different approaches, and choose the one that I think is the best to solve my current problem. Python lets me express myself in code directly in various ways.

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว +6

      Thank you Robert! It’s indeed one of the powers (and dangers) of Python that it allows for so much flexibility.

  • @danyalt8221
    @danyalt8221 4 หลายเดือนก่อน

    One of the greatest videos I've seen from your TH-cam channel, Arjan! Thanks for it.

  • @exganza
    @exganza 2 ปีที่แล้ว +1

    man your channel is underrated!! you are really provide a high quality contents with a great knowledge and experience, partial function are awesome i need to use it more, thank you.

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว

      Thanks Ziad, happy you’re enjoying the content and it was helpful!

  • @smalltimer666
    @smalltimer666 2 ปีที่แล้ว +25

    I really like how your tutorials have never been completely OOPs. A functional-leaning python programmer is a rarity :D I wonder if you have a playlist of videos on functional-ish concepts. The way you describe things is perfectly applicable and would be a very clean and organised way to write julia code as well.

    • @siddsp02
      @siddsp02 2 ปีที่แล้ว +2

      I wouldn't expect a lot of those types of videos due to Python's fairly limited support of functional programming. A few functional modules that are nice to use in the standard library (which might be covered later on) are itertools, operator, and the map, any, all, and filter functions.

    • @smalltimer666
      @smalltimer666 2 ปีที่แล้ว

      @@siddsp02 Thanks!

    • @DistortedV12
      @DistortedV12 2 ปีที่แล้ว

      Would also appreciate a treatment of the functional approach and its strengths. Also, mentioning how to write good functions in practice.

    • @DerekHohls
      @DerekHohls 2 ปีที่แล้ว +6

      Actual of lot of folks who are not programmers but who use Python for basic data processing typically would never use a class at all.

    • @jb_lofi
      @jb_lofi 2 ปีที่แล้ว +1

      @@DerekHohls Yes. I think this tends to get understated -- I've done a lot of Python and PowerShell "programming" for a lot of real-world enterprise uses, and I very, very rarely have to use an OOP approach when coding something from scratch. I rarely have to define my own classes at all; I wouldn't say most/all/anything superlative, but there's a lot of people who understand OOP but just never need to use it Python in that way.

  • @prestonfrasch9270
    @prestonfrasch9270 2 ปีที่แล้ว +1

    Hi Arjan! Thanks for all your work teaching and coding! I came accross partial when I was working with python's multiprocessing library for performing a task with many files with some "standard" parameters that were the same for each and some "variable" parameters (such as the dataframe) that were unique to each process. This video gave me a better context of what partial actually is/does, and it's helping me with an NLP project refactor. 😄

  • @XRay777
    @XRay777 2 ปีที่แล้ว +10

    functools is amazing. Besides partial there is also reduce, wraps and lru_cache which I use all the time. singledispatch can also be nice in very simple cases but it quickly hits a roadblock in more complex scenarios.

  • @rolandovillcaarias5112
    @rolandovillcaarias5112 ปีที่แล้ว

    Thank you for your time and patient, the DOC contains a beautiful explanation in 7 steps about design better.

  • @Jorrit_200
    @Jorrit_200 2 ปีที่แล้ว +15

    I've temporarily moved to Finland from the Netherlands, and I can confirm: the bread is better in the Netherlands.
    There is a even bigger difference in cheese though: the cheese is horrible here.

    • @TheStickofWar
      @TheStickofWar 2 ปีที่แล้ว

      I feel this way about cheese when moving to Norway

    • @ratfuk9340
      @ratfuk9340 8 หลายเดือนก่อน

      Finnish rye bread is unbeatable.

  • @virtualraider
    @virtualraider 2 ปีที่แล้ว +1

    This is one of your best videos yet (and they're all consistently great!) I had to post a comment just to give extra likes 👍👍👍👍👍
    The bits about closures and partial are BOSS 🤓

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว

      Thank you so much!

  • @eduardomoraes2819
    @eduardomoraes2819 2 ปีที่แล้ว

    I’ve been living in the Netherlands for 3 years and you are the first dutch that I know that doesn’t like sliced bread 🤣
    Very good video, as usual!

  • @orenozeri
    @orenozeri 2 ปีที่แล้ว

    Hi Arjan,
    Your content is great, found that one amusing in particular, since my wife is Dutch living in Israel for almost 20 years, while still complaining over the local bread taste and praising the Dutch version 🤣hilarious !!!
    Thanks for the effort you put into the content. 🙏

  • @luandasilva4639
    @luandasilva4639 2 ปีที่แล้ว +3

    Great video as always! Partials are nice. I wrote a validate(validate_func, data_type, data, ....) function that I could then call from a partial like validate_json = partial( validate, json.loads, "JSON")
    or validate_uuid = partial(validate, uuid.UUID4, "UUID").

  • @SUGATORAY
    @SUGATORAY 2 ปีที่แล้ว +1

    So what would be the difference between using closure and functools.partial? When would you prefer to use closure over partial?

  • @FirstLookVaper
    @FirstLookVaper 2 ปีที่แล้ว +1

    you told me to buy Dutch bread after getting the free guide, but I see no link in video description for where to buy it, so I guess I will just keep living in my "bread fantasy world". Love your vids. They are always very informative. Thank you!

  • @bartuslongus
    @bartuslongus 2 ปีที่แล้ว +6

    get_best_bread = partial(get_bread_from, country_code="de_DE") :) Great video! I was astounded seeing the closure part and then really impressed seing partial() in use. The flexibility and simplicity of providing values for named variables and using it on any function is amazing. My wife is gonna get jealous of the love I am developping for Python. Thanks for sharing this!

  • @f0lkien
    @f0lkien 2 ปีที่แล้ว

    Man, i'm programming for 15years or more. Mainly c,cpp, python only 3y, but ive newer knows this closure and partial mechanisms! Great!

  • @markgacoka9704
    @markgacoka9704 2 ปีที่แล้ว

    You have the cleanest code I’ve ever seen in my life!

  • @AnotherAvaibleName
    @AnotherAvaibleName 2 ปีที่แล้ว +2

    Thank you for providing the github repo of the example. Nice!

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว

      You’re welcome!

  • @vladyn2522
    @vladyn2522 2 ปีที่แล้ว

    Thank you for another great video Arjan! also, your anecdote reminded me of a similar pun "no matter how kind your kids are, German kids will always be kinder"

  • @ryanchou2846
    @ryanchou2846 2 ปีที่แล้ว +1

    Is there any difference between using a partial function and a lambda function?
    For some f(x,y), shouldn't lambda x: f(x, y0) be equivalent to partial(f, y=y0) ?

  • @alexanderzikal7244
    @alexanderzikal7244 11 หลายเดือนก่อน

    I like the last example with "partial functions". Still Composition, but very flexible. Thank You again for your teaching!

    • @ArjanCodes
      @ArjanCodes  11 หลายเดือนก่อน

      Thank you for the support, Alexa! Glad you're enjoying the content!

  • @collegeinvestor7095
    @collegeinvestor7095 ปีที่แล้ว

    17:55 where did the partial grab the prices from however if it was not told..?

  • @Whatthetrash
    @Whatthetrash 2 ปีที่แล้ว +1

    While I don't follow everything you do, I like to watch your videos because it exposes me to new ways of thinking as well as showing me the practical reality of what is typed to accomplish an objective. Very cool indeed. :)

  • @lucabaldini8374
    @lucabaldini8374 2 หลายเดือนก่อน

    I want more contents about functools. I love functional programming and i hope you will provide dedicated courses about it

  • @jordansilke3629
    @jordansilke3629 2 ปีที่แล้ว +5

    Very informative! As someone who was previously unaware of partials, I have achieved similar functionality by setting constants and using them as default parameters (as you mentioned). This approach seems cleaner, though, so I will definitely give it a whirl in the future!

  • @LuddeWessen
    @LuddeWessen 2 ปีที่แล้ว +1

    Excellent, as usual!
    The partial function will be used tomorrow, replacing a (very cumbersome) workaround. Thanks👌

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว +1

      Glad to hear it was helpful, Johan!

  • @lawrencedoliveiro9104
    @lawrencedoliveiro9104 2 ปีที่แล้ว +4

    12:35 But you could replace it with a docstring. Which could be dynamically generated, to incorporate the actual limit, and assigned to the __doc__ property of the function object being returned.

  • @flakobatako
    @flakobatako 2 ปีที่แล้ว

    I'm in love with your content. I think I'll purchase your course the SW designer mindset.
    Keep it up my man!

  • @davidlakomski3919
    @davidlakomski3919 2 ปีที่แล้ว

    Nothing really related to your video, but I wanted to thank you for your work. Not only it is very well explained but it really inspires me by debunking some dev "features" and mindset related to development. I don't really study your videos (sometimes still too advanced for me), but they show me every time the beauty of code and keeps me learning further. Furthermore, I tend to really enjoy your both light/humble and technical tone, I'm from marketing, and your channel is doing marketing right focusing on content value first. So again, thank you for your work :)

    • @davidlakomski3919
      @davidlakomski3919 2 ปีที่แล้ว

      Just yesterday, it inspired me to have fun playing with inheritance and composition with PyMongo's source code (not touching at it, but making my way around it for some purpose). Trying to deep dive into code like you do, finding beautiful and fun features, far from just focused tech tutorials, more of a technical case study / learning experience. thx !

  • @georgetait864
    @georgetait864 ปีที่แล้ว

    Do partials have any affect on time complexity of a function? Say you have a function where the part that is partially applied takes O(n^2), and the remaining part would be O(n). When calling the partially applied function, would this be O(n), because the O(n^2) part has already been handled or O(n^2) because it hasn't? This seems quite niche, but im currently working on graphical demonstration of time complexities and partials could be very helpful as I could only look at timing a specific part of a function. I watched this video a while back and remembered how useful I found it

  • @josgibbons6777
    @josgibbons6777 2 ปีที่แล้ว +1

    Someone probably mentioned this already, but if you refactor the functions you're passing to partial so the arguments you're specifying therein are to the left of the one the new function still needs, you don't need to pass them by keyword.

  • @P0lntL3sS
    @P0lntL3sS ปีที่แล้ว

    I'm not sold on closures. Besides encapsulation what are they good for?

  • @multigladiator384
    @multigladiator384 2 ปีที่แล้ว

    repeat and partial can come in really handy when it comes to multithreading and passing arguments to the executed function

  • @ankurgaikwad7252
    @ankurgaikwad7252 ปีที่แล้ว

    What is the name of the theme you use in VS Code? I love the colour scheme of syntax highlighting in your videos.

  • @petermleigh
    @petermleigh 2 ปีที่แล้ว +3

    Great video. I've started to use partial more and more in my functional code. I was using lambda for the same result, but partial is a lot better.

    • @martinleonhardt1541
      @martinleonhardt1541 2 ปีที่แล้ว

      Interesting! What are the perks? In short perhaps...

    • @petermleigh
      @petermleigh 2 ปีที่แล้ว

      @@martinleonhardt1541 Partial will work around mutable types and closure better. Lambda got me in a pickle when I tried using it in a list comprehension.

  • @iliqnew
    @iliqnew 2 ปีที่แล้ว +1

    Once again, Arjan! Super helpful. Thanks!

  • @oncedidactic
    @oncedidactic 2 ปีที่แล้ว +1

    I constantly write code that starts dead simple and expands in options progressively, ending up with very customizable functions. I’m gonna use partials to take care of this now!

  • @nedegt1877
    @nedegt1877 2 ปีที่แล้ว

    Bedankt! Thank you, your video's are really helping me getting the most out of python and I really appreciate that as a self taught Pythonista :)

  • @phxz_stonks
    @phxz_stonks 2 ปีที่แล้ว

    Great content, I work in the Netherlands as a Python Backend Dev and and the bread is okish haha!

  • @Seawolf159
    @Seawolf159 ปีที่แล้ว +1

    You said 35_000_00 is thirty five thousand, but you also said that the underscores doesn't change anything about the integer value so it would be 3500000 so that wouldn't add 2 decimals randomly? So it should be 3_500_000 which is 3,5 million?

  • @wernerlippert5499
    @wernerlippert5499 2 ปีที่แล้ว +1

    No, no, no - you definitely did not fall victim to the Dunning-Kruger effect (about bread), you just have a very good sense of humor.

  • @JayJay-ki4mi
    @JayJay-ki4mi 4 หลายเดือนก่อน

    This is also a great example of where you could also use a state machine :)

  • @fabhi
    @fabhi 2 ปีที่แล้ว +4

    Love your videos man

  • @Megyez
    @Megyez 2 ปีที่แล้ว

    How would you do dependency injection for functions?
    For instance I have several functions where I want to use a database, for this I have a class or function which I want to re-use and mock for unit tests.
    If I use classes I would inject this dependency in the constructor. In the composition root I would assemble my objects. If I would do the same on function level for instance with te partial() it sounds like a lot of configuration, and a bit unclear for me how to structure the code then.

  • @volbla
    @volbla 2 ปีที่แล้ว +1

    The `key` argument in list.sort() only accepts functions with a single input. Partial came to my rescue when i wanted to do some funky sorting.

  • @martinbrader
    @martinbrader 2 ปีที่แล้ว

    Thanks

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว

      Thank you Martin, glad you liked the video!

  • @trainchen9861
    @trainchen9861 10 หลายเดือนก่อน

    Thanks!

    • @ArjanCodes
      @ArjanCodes  10 หลายเดือนก่อน +1

      Thank you so much for the support! I really appreciate it!

    • @trainchen9861
      @trainchen9861 10 หลายเดือนก่อน

      @@ArjanCodes I am a developer from Taiwan( in Asia). I have been writing Scala code as a data engineer for the last three years. Recently I joined a new company and my team uses Python for everything. And I struggle to accustom myself to the new environment. Watching your videos about Python’s usage and how to write code in a more functional programming way makes me more confident and more productive. Thanks for the effort!

  • @matthewb3853
    @matthewb3853 2 ปีที่แล้ว +3

    Why did he put the extra 2 zeros on 35_000_00 ? It evaluates in my interpreter as 3,500,000 when I'm sure he meant 35,000. Could someone please explain what I'm missing?

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว +6

      I’m using the integer type to represent a monetary amount, and then the common way to do it is to use cents as the base unit and not dollars.

  • @rtiodev
    @rtiodev 2 ปีที่แล้ว +2

    Great video as always. Keep going!

  • @jeancharlesmourey
    @jeancharlesmourey 2 ปีที่แล้ว +7

    French bread is of course better by far than any other bread, but I guess it wouldn’t be fair to include it in the competition as there wouldn’t be any controversy.

    • @jasonmcclatchie6877
      @jasonmcclatchie6877 2 ปีที่แล้ว

      I am English and I'm still willing to back you up on the superiority of French bread!

    • @obed818
      @obed818 2 ปีที่แล้ว

      @@jasonmcclatchie6877 thanks hehe

  • @PanduPoluan
    @PanduPoluan ปีที่แล้ว

    My mother used to make bread with recipes that undoubtedly came from the Netherlands (as my country was a Dutch colony and before that we have no bread) and I'll say they're great! 👍🏽

  • @elidc93
    @elidc93 2 ปีที่แล้ว

    I rarely comment on video tutorials, unless it gave me that "aha" moment, and this was one of those, thanks ArjanCodes for educating us!
    btw, have you profile which "way" is more faster, the OOP or FP style?

  • @alvaroe2704
    @alvaroe2704 2 ปีที่แล้ว

    I have studied industrial engineering in Spain, with little curriculum in programming. Is it possible that I prefer functional programming due to my background in engineering? I.e. decompose the 'problem' in little steps via functions and then there is a main function that calls functions which in turn call lower level functions...and so on.

  • @comedyclub333
    @comedyclub333 2 ปีที่แล้ว +12

    While I use functools in many occasions I always wondered what the (technical) difference is between the partial function and creating a lambda (or even a function) simply reducing the arguments? I mean, i could also do
    buy_strategy = lambda prices: should_buy_average(prices, 4)
    While "partial" usually increases readability I wondered if there are any differences in terms of performance.

    • @StanislavStratiev
      @StanislavStratiev 2 ปีที่แล้ว

      I am also interested in this.

    • @marsma18
      @marsma18 2 ปีที่แล้ว

      Doing that, you use closure, but in simpler form.

    • @comedyclub333
      @comedyclub333 2 ปีที่แล้ว +3

      @@marsma18 kind of, yes, but not in the way he showed closures in the video as my approach IS a function with a valid signature actually calling another function while the approach in the video RETURNS a newly generated function with a valid signature. Moreover I was interested in the technical difference than the logical difference.

    • @RitchieDiamond
      @RitchieDiamond 2 ปีที่แล้ว +2

      @@comedyclub333 The only real difference that I came across was while implementing a naive save system in my game engine (naive in the sense that I just dump whole objects, including attached callback functions and everything to file using pickle). Now callback functions cannot typically have arguments, but often require a variety of them, so I end up using functools.partial fairly often - because lambdas are anonymous and cannot be pickled, while partial functions can.

    • @comedyclub333
      @comedyclub333 2 ปีที่แล้ว

      @@RitchieDiamond That's actually something really interesting. I just tried it and you are right! It seems that lambdas really are not savable into serialized data. Crazy!

  • @alfonsov3190
    @alfonsov3190 2 ปีที่แล้ว +1

    I sure learn a lot from your videos. Thank you so much!

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว +1

      You’re welcome, Alfonso!

  • @cmdlp4178
    @cmdlp4178 10 หลายเดือนก่อน

    Very useful if you want to write to a file using print:
    with open("output.txt", "w") as f:
    fprint = partial(print, file=f)
    fprint("Hello world!")
    Same for printing to stderr
    import sys
    eprint = partial(print, file=sys.stderr)
    ...
    eprint(f"Error occurred: {error.description}")

  • @michaelbamiloshin1670
    @michaelbamiloshin1670 2 ปีที่แล้ว +2

    Even if I'm not yet at the level where I have to implement most of the things you share, I still go ahead to watch your videos anyway. You teach well.
    That said, I thought I should point out something to you (and to those not familiar with this style). At minute 12:51, you add two additional zeroes, presumably to make the number 35,000. But that actually changes the number to 3,500,000. (And I confirmed this on my python shell.)
    35_000_00 != 35,000
    Instead,
    35_000_00 == 3,500,000
    35_000 == 35,000
    3_4 == 34 (just to show those who might not know that you don't always have to end with zeroes)
    In essence, it seems to me you think adding two zeroes after the last underscore means python will treat the variable as a float. It won't. It'll just add two more zeroes to the integer.
    If you want a floating point number:
    35_000.00
    1_2.3
    12_34_567_8009.027
    are all different valid examples.

    • @jjenn050
      @jjenn050 2 ปีที่แล้ว +3

      Money is best dealt with in integers because floats will have errors dividing in binary. It is 35000.00. When returning values, you just have to deal with the extra 00 as cents.

  • @todorowael
    @todorowael ปีที่แล้ว

    Thank you for the great tutorial and all the interesting Python content on your channel!

    • @ArjanCodes
      @ArjanCodes  ปีที่แล้ว

      Glad you like them!

  • @Betacak3
    @Betacak3 2 ปีที่แล้ว +12

    I'm German and I'm eating delicious bread as I'm watching this and I'm highly offended.

  • @omerpriel5588
    @omerpriel5588 ปีที่แล้ว

    Like many good things. It's really convenient and right when your team knows each other about it.

  • @tienalan
    @tienalan 2 ปีที่แล้ว

    Why did you decide to use dataclass for TradingBot?

  • @littlegiant4400
    @littlegiant4400 2 ปีที่แล้ว

    Hi, Your videos are awesome... one question though.
    How do you place that run button beside the split editor right icon in vs code, are you using any plugins/extensions?

  • @iklintsov
    @iklintsov 2 ปีที่แล้ว +1

    Man you are the best! Could you make a video about design patterns of libraries like django or scrapy?

  • @mlanc7775
    @mlanc7775 2 ปีที่แล้ว

    I'm a bit confused. in your dataclass you are defining buy_strategy as a TradingStrategyFunction which you defined as that Callable.. But what's the difference between that and just calling the definition directly from within the class? Without self.function_name() just function_name()?

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว

      The reason I'm using a function and not a method, is that by defining them as functions, you can set them when you create the object (this is what I do in the main function). If the buy/sell strategies were methods, you would need to create a subclass for each combination of buy/sell strategy you want, so it's less flexible.

  • @jonataseduardo
    @jonataseduardo 2 ปีที่แล้ว

    Great video! Its begs the question of when a functional approach is better then a objected oriented one.

  • @marcdelabarreraibardalet4754
    @marcdelabarreraibardalet4754 2 ปีที่แล้ว

    Nice video as usual! Question on partial. Before knowing about it, I used to do:
    buy_strategy = lambda prices: should_buy_average(prices, window_size=4)
    vs
    buy_strategy = partial(should_buy_average, window_size = 4)
    I think the result is the same. Are these methods equivalent, but partial with less code?

    • @chriswunder5420
      @chriswunder5420 2 ปีที่แล้ว +1

      There are a few differences I guess. For example with partial you can change the should_buy_average function (as long as it still takes the window_size) and need not change the partial statement, whereas you would need to change the lambda statement because it explicitly uses the parameters the should_buy_average function expects.
      You can also change the parameters' order of the function and the partial application still works.

  • @davidbeauchesne7399
    @davidbeauchesne7399 2 ปีที่แล้ว

    Nice! Thanks for another great tutorial.

  • @pierremonico5002
    @pierremonico5002 2 ปีที่แล้ว

    What is the purpose of using a dataclass over a normal class for the TradingBot? Seems odd to me, so genuinely asking.

  • @nickhodgskin
    @nickhodgskin 2 ปีที่แล้ว

    Love your videos Arjan!

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว

      Thanks Nick, glad you like them!

  • @ShubhamMishra-gc5ow
    @ShubhamMishra-gc5ow 2 ปีที่แล้ว

    Hi, please make series of advance level python. Your way of explanation is very cool and the set of example you used is very cool.

  • @sillytechy
    @sillytechy 2 ปีที่แล้ว +2

    Hi Arjan, Please continue the Design Pattern Series, You can take one by one different design patterns with practical code for python. There is not much good content out in youtube.

  • @NicholasShanks
    @NicholasShanks 2 ปีที่แล้ว

    I'm interested in what keys you're pressing. How do you: 6:06 forward-delete an argument including the following comma and space up to the next argument? 15:31 delete a whole line at a time incl. LF? 15:49 delete to the end of the line? 17:59 forward-delete up to the end of the argument list?
    Also British people know that our bread is the worst so we don't even pretend it's good.

  • @Julie9009
    @Julie9009 2 ปีที่แล้ว

    Hi Arjan. Thank you for the instructions on passing extra params using closures. I bookmarked this video when I first watched it because I suspected that it would be useful, and today it was exactly what I needed to add functionality to the callback function passed to ftplib.retrbinary()

  • @lawrencedoliveiro9104
    @lawrencedoliveiro9104 2 ปีที่แล้ว

    10:38 I like to use a name like “def_should_buy_avg”. The extra “def” on the front looks like the keyword for defining a function -- which is what the function does.

  • @amolsv
    @amolsv 2 ปีที่แล้ว

    What is "recording" on every line of your terminal?

  • @ravenecho2410
    @ravenecho2410 9 หลายเดือนก่อน

    Functional patterns are awesome and one of my favourite ways to structure code, u see it a lot in scala or heaven forbid R (their class like defns are terribad)
    But scalas map, filter, reduce, foldleft, currying or partial hell maybe i just used clojures and they got tail recursive.
    I had all of my steps be like functional but isolated environments be classes.
    I think both are cool

  • @thomasferreiradelima2324
    @thomasferreiradelima2324 2 ปีที่แล้ว

    Hey @ArjanCodes, unrelated question: What's this sweater you're wearing? Thanks!

  • @HexenzirkelZuluhed
    @HexenzirkelZuluhed 2 ปีที่แล้ว

    I've been using partial() for similar applications before. Well explained.

  • @DoubleT847
    @DoubleT847 2 ปีที่แล้ว

    I have just discovered the channel. This is Golden

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว

      Thank you, glad you like the content!

  • @mikeciul8599
    @mikeciul8599 ปีที่แล้ว

    I just discovered a disappointing downside of functools partial - partially applied functions don't typecheck!
    I use mypy to check my type hints. I have a type hint for a "validate_multiple" function that expects a list of Callables with a certain validator signature. I tried to pass a partial to it and got this error:
    Argument 3 to "validate_multiple" has incompatible type "List[object]"; expected "Sequence[Callable[[Dict[Any, Any], ExecutionContext], Dict[Any, Any]]]
    Apparently there's an ugly workaround that involves manually casting the partial back to the right type, but that's not satisfying at all. :(

  • @rogifedi1
    @rogifedi1 2 ปีที่แล้ว +1

    Good stuff, thanks for this great video!

  • @lebesgue-integral
    @lebesgue-integral 2 ปีที่แล้ว

    Yeah, I'm liking your videos. I'm not a software engineering but I use python a lot to do data science.

  • @linuxmill
    @linuxmill ปีที่แล้ว

    I'm from the US, and I can vouch for the excellent NL accent! ...and I'll grant you the bread assertion

  • @grokes_notes
    @grokes_notes 2 ปีที่แล้ว

    I totally agree about bread, I think the Polish one is the best ;) Anyway, another great tutorial, thanks!

  • @katzy687
    @katzy687 2 ปีที่แล้ว

    Forget where I saw it on youtube, but someone commented that most classes are basically "collections of partially applied functions". In that scenario where I have similar methods, I would rather have user instantiate a class once than do the partial applications repetitively for the different functions / methods

  • @javibits
    @javibits 2 ปีที่แล้ว

    Always learning something from your videos. Great content

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว

      Awesome! Thank you!

  • @uniquenameification
    @uniquenameification 2 ปีที่แล้ว

    Nice! I’ve been writing Python for ages and didn’t realize partial was in there. Re:bread you need to try a good shokupan …

  • @davia.sampaio8633
    @davia.sampaio8633 2 ปีที่แล้ว

    Have I ever said how much I like your videos?

  • @danny_p466
    @danny_p466 ปีที่แล้ว

    It's nice that you can replace interfaces and their implementations with partial functions. However I'm still concerned with what happens in cases where we need to do some error validation and type checking, any ideas?

  • @Eriddoch
    @Eriddoch 2 ปีที่แล้ว

    Does partial() mess up autocompletion?

  • @demonhunter2121
    @demonhunter2121 2 ปีที่แล้ว

    I think we need an ArjanBakes channel where you show off Dutch bread

  • @getpoked101
    @getpoked101 2 ปีที่แล้ว

    Hey. Speaking of design guide. I am signed up and get your notices at work but never got the guide. Is there a way to remove-request or access it ?

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว

      Hi Greg, send me an email (business@arjancodes.com) and I’ll sort it out for you.

    • @getpoked101
      @getpoked101 2 ปีที่แล้ว

      Will do

  • @havard3815
    @havard3815 2 ปีที่แล้ว

    Isn't it possible to use lambda functions instead of partial?