Python Return Statement Best Practices and Working With the map() | Real Python Podcast #31
Skills:
Python for Data80%
Key Takeaways
Covers best practices for Python return statements and functional programming with the map function
Full Transcript
welcome to the real python podcast this is episode 31. the python return statement is such a fundamental part of writing functions is it possible you missed some best practices when writing your own return statements this week on the show david amos returns with another batch of pie coders weekly articles and projects we also talk about functional programming again with an article on the python map function and processing iterables without a loop we cover several other articles and projects from the python community including interactive data visualization with pi gal everything you need to know about named tuples pep638 syntactic macros python for kids the new nvidia jetson board and a reinforcement learning project named football alright let's get started [Music] the real python podcast is a weekly conversation about using python in the real world my name is christopher bailey your host each week we feature interviews with experts in the community and discussions about the topics articles and courses found at realpython.com after the podcast join us and learn real-world python skills with a community of experts at realpython.com hey david welcome back hey chris yeah it's good to be back again we're going to dive into a variety of articles again i was going to start off this week i have i think i've mentioned multiple times that i'm into data visualizations and this one came up in pi coders and i was like oh this looks really cool it's titled interactive data visualizations in python with pi gal and i had not heard of this library it's on the towards data science medium blog this particular one is by sarah metwali talking about creating these not only visualizations but one of my favorite things especially if anybody's looked at my bokeh course is i really like interactive graphs and charts stuff where you can filter stuff out or you know remove parts of the data and showing it and this library looks like it does a really good job of that and then it has a really unique sort of feature to it that i was kind of surprised by it exports svgs and i've seen svgs which are the scalable vector graphics and they're great because it like anything vector the resolution can be scaled up and down and they print really nice and you don't get that kind of blocky raster kind of thing that you might have to deal with especially with like you know computers today everything's like switching to retina type displays double times displays and things like that and the idea of something that can kind of go across that that's described by text you know that can be scaled into graphics is very intriguing to me and then what i didn't know a lot about svgs is that they can be interactive that you can have a thing where as you hover your mouse over it it'll highlight maybe it's a map or an area of a graph and can then do the hover showing values inside of it but then also you can kind of click on different entries say in a list of things in a graph and so it's it's a neat library one concern i have about it is that it is a little old it looks like most of the main updates to it were done in 2016 or 2017 so i'm not sure what the status is right now of the project but maybe this renewed focus will get some more attention on it i like how it looks it definitely has a lot of the you know all the standard bar charts and maps tree maps which i'm not that familiar with doing the article goes into taking data very popular at this time of covet data and and kind of spreading it out across this case counties in different states and and showing the different data you know if you're interested in data visualizations and you're looking at possibly different ways that you could present it the idea of being able to export out these svgs and be able to embed them maybe in a flask application or django or just being able to you know embed them just within html is pretty powerful and so i definitely think it's worth checking out they go through you know everything like i was mentioning pie charts they do this thing called the you know the gauge charts that are sort of a donut shape and then lots of styling yeah yeah with lots of colors and it's definitely worth checking out as another potential graphing library and then i'm really excited about the idea of additional ways of saving the graphs as these svg files because i think that could help with the not only the interactivity but also just the idea that it can kind of scale to whatever kind of display or div or whatever you stick it into yeah it was so unique because one of the reasons i wanted to feature this article and and picoters yeah because of this idea of these interactive svgs i hadn't explored at all i was kind of surprised that you could even do it although i mean in retrospect it's like it's not that surprising once you sort of like peel apart the way svg's work and everything but yeah yeah it just seems like uh she says at the beginning of the article you know it just gets overlooked this pie gal when people talk about plotting libraries and you know i think the fact that it hasn't been maintained in a while the i'm looking at the github right now the last push to master i don't know about the latest release but the last push to master was july 17 2018 so yeah okay over two years ago yeah and i don't even know if that's like you know if that's whatever that was was in a release the latest release that's like on pipeyi might be even older so that is one reason you know you may not want to use it especially in something that like heavily relies on you know it's like running in production and relies on code that's going to be secure and well maintained and everything yeah it makes me think about all the all the things of coming out with python39 and right also idea of making your code maintainable and updated and so forth but it looks like a really cool library and maybe you know someone will be inspired to maybe help out and and see if it's worth uh kind of dusting off and yeah getting back up to speed so yeah it's definitely a cool thing and i i too had never seen the the needle charts i thought those were pretty interesting looks like a gauge like on on like your car or something right like uh right right i think that's the point i think that's how you might might use it or a clock or something but yeah right yeah it's a little different in that sense but yeah good stuff so what do you got first first one i've got is an article about the map function from real python it's by leo danis poso ramos which we featured several of his articles on this on this podcast now and i think there'll be even more coming up in the future but the article is called python's map processing iterables without a loop so it's a very thorough article which most of his are very thorough and it talks about how you use the map function and it gets into even some really interesting things so he talks about you know this map function to i guess to sum it up you know it it allows you to process and transform items in an iterable without using like a for loop so you pass the iterable and some function that that you that gets applied to every item in the iterable to the map function and it returns a new iterable with that function applied to each item so it's it's like a transformation and you don't have to write a loop to do it that's i guess one of the draws to it it's one of the tools that helps support like a functional programming style which we've talked a little bit about yep before dove into that before yeah one of the nice things about this article is it kind of gives you really gives you a lot of context so like how this fits in to the standard library why it's there and why you know it you might use it or not use it what the alternatives are so it's really a very comprehensive guide he talks about how guido had never really guido is not i don't think a big fan i don't know this for a fact but just like kind of anecdotal evidence and things i've read it seemed to kind of point to like guido not being a huge fan of maybe the functional programming style and you know he's got this quote that he says i've never considered python to be heavily influenced by functional languages no matter what people say or think i was much more familiar with imperative languages such as c in algol 68 and although i had made functions first class objects i didn't view python as a functional programming language but back in 1993 the python community started kind of demanding some functional programming features so you got things like the lambda you got the map function the filter function the reduce function leodanis does a good job of providing this context to kind of you know help you understand why this is there and then how to get started with it so it covers just the basics of using the map function understanding how it works and kind of comparing it to for loops and everything so you kind of see what's i guess a way to think about how map works in terms of your like the for loop which is a good way to to kind of think about it but he gets into some other stuff so he talks about a version of map that exists in the itter tools package which is one of my favorite packages in the in the standard library and that's called the starmap function which is similar to similar to map but that when you call the function on on an item in the iterable it applies the unpacking operator so if you have like an iterable of iterables right so like a list of lists or something sure okay then and you have a function that takes multiple arguments so basically what you do is when the function gets applied to an item in the iterable which is a list then the star operator the unpacking operator gets applied and each item in that list gets becomes like one of the arguments of the function so in one of the limitations of the standard map function is that the function that you pass to map has to be like it has to take a single parameter or have a single parameter because whatever the item is in the interval just gets passed to that function as an argument so if the function expects more than one argument you're going to get an error the star map though you can have a function that takes multiple arguments and use it in the same thing so that's kind of the difference there so he talks about star map and then he talks about things like list comprehensions and generator expressions and how you can achieve the same results as you would with map using these other constructs and they may be considered maybe more pythonic they might might be easier to read that's you know an opinion but it just gives you the whole gamut of of kind of everything out there on on maps so it's very thorough and very well written lots of great information in it yeah cool i i got to talk to him the other day yeah from cuba which is very cool and so we talked a little bit about his background coming from being you know working in petroleum and being a mechanical engineer and it's interesting because he has a similar background to me in some ways that he's very self-taught it's just sort of bootstrapped his way into python this is a really been a fantastic way for him to not only research these topics that he's covering in these articles but also just to like you know learn in that process by like you know fanning out and looking at all the different ways that you could kind of go into that so which has been i mean it's been fantastic for us so at real python yeah absolutely and another thing just you know leodon is if you're you're listening i mean i'm just going to fill this out there because i i talked with him too as well and and he has been learning english i guess for like about a decade but has never really gotten a lot of practice speaking or writing english i guess before he started writing for real python which is mind-boggling to me because he's a really great writer so that's uh i mean that's amazing so great job on that there dotties you're writing great english it's awesome yeah i want to explore this article more and and kind of look at i don't know anything to do with iterables has kind of been a thing that i've been focusing on a little bit in my programming to like like you said like additional ways to kind of create iterables and then then look at the different types of collections and tie it all together yeah for sure so my next one is kind of a call back a little bit to a topic that i talked about i was mentioning dan's article about common python data structures and one of the ones that we kind of passed through as we're going through this massive list of all the different types were not only the data class that came along in our data classes that came along in python 37 but also the collections named tuple our name tuple and some of the advantages to that and so when i saw this article i was like okay i want to dive more into that and the article is called everything you need to know about python's name tuples and it's by miguel brito i guess it's called me gendez megindez's blog if i'm pronouncing that correctly kind of just dives into the idea of like okay why would you want to use these what's what are advantages to them and kind of does this real contrast between creating data in dictionaries and why a name tuple would be advantageous in that sense they're created in a kind of a funky way like a lot of people may know that when you are putting things into a function that what's being put in is usually a tuple you know even though it might be a single item you know all those different arguments that are going in inside the parentheses and also what is returned is typically a tuple but what this will do is create this new sort of object that in some ways kind of feels a little bit like a class the way that you kind of create them so you import it in from the collections module and then from there you do something like okay you know car equals name tuple parentheses and then instead of like having to say the way you would define a class or define a function you just simply put in a single text string for kind of what the name tuple you kind of almost repeat the name is a very common method of you know applying this and then the next parameter you put in there is you know all the names of the things that you want to do and that's usually like a text string in this car example it could be color space mileage space automatic you know inside of a pair of quotation marks and then when you go to create the cars like car one you'd say okay car one equals car and then you simply pass in the three different values that you want in there so if it was color you'd say okay red comma and then the mileage whatever the current mileage is is like a float and then on if it's automatic or not i'm gonna keep it like say true and so that creates this object and what's nice is that has that notation that dot notation that you'd be used to with a class so you can you can get to those values by saying okay car one dot color and it actually would then in this case show the value that it's red or you know if you said mileage or what have you but you can still use indexing if you want but indexing you know it's harder for people to to read and see that so in some ways it feels like a class now here's where it's really different though right is that the fact that it you can get to those values but you can't set them right so they're it's an immutable object you can basically create them but they're going to be in changeable and another nice advantage is that the size of them they're small right they're very much like a tuple as far as if you use the uh get size of to look at the different values you'd see that they as far as memory management they're going to be about the same as a tuple which is great and another advantage or disadvantage being how you look at it is that they're hashable unlike a dictionary right so so there's a whole bunch of different advantages that kind of goes into here and then he goes into how could you have these behave with dictionaries how can you unpack values coming back and forth between them creating them assorting the tuples if you wanted to take a group of name tuples how could you serialize them into json and then some kind of interesting things like to to make them you know even feel a little more like a class like this this idea of adding a doc string to it and so it's a neat article kind of diving into you know some of the deeper things that you may be interested in being able to do with this behind the curtain tool and a lot of people may not know about them but you know the more that i see them in use i'm like oh these could be really really powerful and then we've been talking a lot about optimizing code you know things like for data science and so forth the idea of having something that like this that could potentially replace a dictionary and have a smaller footprint and not necessarily have the you know issues that you might have with dictionaries the mutability and so forth could be you know really powerful so i i don't know it's a good article builds on top of the stuff that we covered in dan's article previously yeah one of the things i really like about this article is that it has a whole section on the differences between name tuples and data classes which i think is a really you know if you're going to have a complete article on name tuples you have to include this because it's there's i guess the sentiment i've seen uh coming from some people and on twitter and and some comments that i've seen on articles that they kind of feel like with the introduction of data classes in 3.7 name tuples are obsolete and that is absolutely not the case there's a very big difference between name tuples and data classes and that is immutable structure versus mutable structure right and it kind of comes down to the difference between like a tuple and a dictionary in a lot of ways so so i don't think that name tuples are obsolete at all in fact the the whole tuple versus dictionary thing is actually a very relevant comparison in my mind because you know you don't say that tuples are obsolete because we have dictionaries right so uh they're serving you know kind of different different purposes and i think you know the the fact that you get a immutable and hashable data structure out of a name tuple is really advantageous in certain situations the fact that they have a smaller memory footprint they're based on code written in c instead of your python right so the speed thing again yeah so there's like some speed advantages which you know on an individual basis isn't it's not like you're getting mega differences between those those two things right but if you're having to process millions of items right that's always what it comes to right it could you know it could mean quite a large difference in the performance of your program so so yeah it's just something to keep in mind that uh think about if if you're using a data class which i think in many instances you should you might want to consider using a name tuple instead if you need some of the advantages that name tuples provide so and that hashing and immutability kind of being the key key thing there right so what's your next one uh next thing on my list is a new pep that is still in draft mode or draft status okay it was created september 24th this year so it's very new still by authored by mark shannon and it's uh the number is 638 and it's called syntactic macros and this you know i have no idea i didn't actually have time unfortunately to sort of i want to dig into some of the debate that maybe has been sprung up around this and kind of look at the mailing list and just see what the discussion going on there was but i imagine there's a lot of discussion going on because it's a very interesting pep in that what it aims to do is support something called a syntactic macro for python so i'm going to read the description from the pep here from its abstract a macro is a compile time function that transforms a part of the program to allow functionality that cannot be expressed cleanly in normal library code the term syntactic means that this sort of macro operates on the program syntax tree this reduces the chance of mistranslation that can happen with text-based substitution macros and allows the implementation of hygienic macros so there's a lot to unpack there we won't get into all those details here's kind of the main summary syntactic macros allow libraries to modify the abstract syntax tree during compilation providing the ability to extend the language for specific domains without adding to complexity to the language as a whole so i guess in more layman's terms what this means is you can create a macro that introduces new syntax to python once it's been compiled to bytecode and then use that syntax in your program so the whole deal with let's say the walrus operator though it was a kind of a big controversy right around all that in fact it led to basically of guido stepping down as as sort of the sole decision maker for all that and we now have the steering council right i mean it led to pretty dramatic changes in the organization of how how decisions get made for the python language so what this is introducing is this would say hey instead of adding something like the walrus operator to the language itself let's make it a macro that you could use if you want right it would be an optional thing you'd you'd download some you'd install some third-party library and import that syntax basically import something that would allow you then to use that syntax and then once you ran your program that would be compiled and that syntax would become valid for that program you'd be able to uh to run it it's a really interesting idea because there is a very compelling argument to not wanting to introduce new complexity to the language and that's what these things like the walrus operator things like this match yeah statement well i think it's six pep 622 that we've talked about all these things introduce new complexity to the language that doesn't exist already and that new complexity can cause issues that aren't easily foreseen so which is why you have you know alpha and beta releases and things get tested and yeah you try to get all these uh get a bunch of things tested before you go into production but there's not always enough time to do that and so he points to a couple of issues with let me see if i can find it here with i think it was the with statement so there were some things i believe it was the with statement that when it was introduced it had some bugs some pretty serious bugs that took time to iron out because they didn't really have the time to appropriately i guess vet that stuff and test it before it went into the language so with a macro you'd be able to test that without actually changing the language at all you'd be able to put it out there you'd be able to prototype this stuff make it really easy for people to you know rather than having to like create a custom version of c python to test out some new syntax you just install this macro and and be able to test it out and then if you know it was stable enough and a lot of people liked it then you could have the discussion i guess of should we add it to the to the language so that's one use case another kind of the main use case that that mark shannon points out is for this is domain specific macros so there's certain patterns that might make a lot of sense for certain domains like maybe in scientific computing or data processing that don't make a lot of sense in something like web development right or something like that so rather than i guess catering to a subset of the users and introducing new complexity on behalf on their behalf and you might introduce a bug that affects people and other user groups then you know this macro idea might make a lot of sense and they can sort of have their own domain specific macros that they use and love and it works for them but they don't have to add that additional complexity to the language itself so it's a really fascinating pep and i'm very curious to see where all this goes from here yeah a couple things on that one when i had russell keith mcgeon we talked a little bit about the the size of python right yeah the actual things and the whole idea of of the batteries included kind of model right and he his statement was and again i'm fairly new to the community but i guess this is a thing that's kind of out there that people say well some of those batteries are leaking pretty badly right and so the idea of adding all these additional features and the other thing that we talked about last week on the python39 episode was this idea of a first party library which i guess this would kind of be in that mode maybe somewhat the idea of that that you'd be adding these things in to your setup if you're maybe you're gonna do stuff with right like a circuit python or like you said like you know data science or what have you they talk about this idea of it being just you know it's fully backwards compatible which is interesting too the idea that you can kind of like structure things in that way so yeah it's one to watch definitely yeah for sure i'm just really curious to see where all this goes i i i really like the idea it makes a lot of sense to me so you know i'm kind of for it at this point just based on reading the pep and kind of thinking about it but like i said i haven't had time to really dig into any of the debates that are going on around it and kind of see what uh what the other viewpoints are and what the other arguments are maybe against it so right right keep an eye on it and see how it goes this week i want to shine a spotlight on another real python video course it's called command line interfaces in python it's based on a real python article by andre burgod in the course instructor liam pulcifer explains the details behind implementing your own command line interfaces in python and how they can process a variety of arguments in the video course you'll learn about the origins of python command line arguments standards guiding the design of a cli the basics to manually customize and handle python command line arguments and you'll learn about libraries available in python and third parties to ease the development of a complex cli of your own i think it's a worthy investment of your time to learn how to implement your own command line interface and how to handle a variety of arguments to make them flexible and more useful to the users of your python projects like most the video courses on real python the course is broken into easily consumable sections and it includes code samples for the techniques shown this course also includes a shiny new transcript and closed captions check out the video course you can find a link in the show notes or you can find it using the enhanced search tool on realpython.com [Music] guess who my next one's from it's another real python article and leodonies has been on a role and so he uh has this one called the python return statement uh usage and best practices and this is a real deep dive you know it starts out with what's a function and the idea of a insurance statement inside of a function and the idea that right even if you make a function that doesn't have a return statement it's still returning none it's returning something it's returning the you know a nun value and you know so it goes through that whole those sort of fundamentals but really sort of explaining what's happening as you're going along and really unpacking a lot of these concepts like returning versus printing yeah the idea of returning multiple values and then it has this whole best practices section that i think is excellent should you return none explicitly do you you know a lot of people may feel like well i don't need to you know the idea of readability inside of that the idea of avoiding sort of complex expressions the way that it may be useful to break things down because you can have like a whole huge expression after you say return you know you could have this very long list comprehension or what have you some kind of very compound statement that that's after that and it you may make sense that inside of your return inside of your function to have a lot of that stuff sort of defined above so right add that readability and yeah yeah like it's into testability and maintainability and stuff but yeah yeah right well that and uh the other thing he mentioned which is the idea of debugging which i still want to get somebody on to talk in a deep level about debugging but the idea that you know stepping through your code and being able to check your values as you go right that's hard to do with a con you know compound you know elaborate exactly expression at that point so so advantages there and then using conditionals uh having multiple steps as you're kind of going through you know with as you walk through the return statement can be kind of short-circuited and leaving the function kind of early as it hits these different things as you're walking through working with true and false recognizing dead code that's something i talked with uh savannah about which was a kind of neat feature of pylance the idea that it can kind of look through your code and see like if you have a return statement early on right you know things can be analyzed to say yeah this never runs you know and then returning multiple things which i think i said but this one actually uses an example of returning multiple named objects and then comes back to the name tubal again which is kind of cool and how that you could use that to define things coming out of it and then have all those nice you know not only the values but the actual you know names of what the values are inside of which is a useful structure and then a couple things that have been kind of foggy in my own learning uh these areas that that you know there's a lot of uh you know cs sort of jargon that gets thrown around and one of them's for me was was closures like it wasn't very clear to me what that is and this really cleared up a lot of what what that means and how that how that works you know there's a lot of object oriented kind of stuff happening inside there also so the factory pattern you know the idea that an object can create other objects so in this case you know you could have a function that literally returns functions or potentially could be returning you know lots of different objects of different types so an example would be like a shape generator and you can you know what do you want it to generate you know do you want to create a square or circle and calling those different things out of there which is pretty powerful and then you know how you can how return sort of works inside of try and finally blocks and then finally you know kind of goes into decorators and then uh the idea of you know how generator functions can work and it's sort of returning each time it's calling those uh next values which is cool it like most of his articles is very very thorough you might think to yourself oh as i read this first half of it i know all this well it has to be covered in order to really explain the things that are happening in the second half of it which is right really diving into some of these things that might be a little fuzzy especially for intermediate developers so i really like that about it uh again i i think he's using it in a way to help him you know clarify things in his mind and and clarify it for our readers and people enjoying the site yeah it's um this is i think a really good article and you know i was kind of surprised when when i saw it coming down the pipeline it's like the return statement you know i'm thinking like what would i you know what would i expect to see in that article what do you have to add and when i saw the outline and everything it was like oh wow this is uh this is going a lot deeper than i kind of expected but the more i think about it the more i was like you know this this is actually highly valuable because there's a lot of especially for self-taught programmers because yeah there's so many subtleties that exist and all this stuff that you know i'm not even sure many cs students really fully grasp this this stuff when they go through like a cs program because you learn about a lot of these things right but i don't know that it necessarily sticks with everyone all of it at the same time too right you know right exactly so this is really you know you probably if you learned how to write a function and return a value then you know you know probably 70 to 80 of what you need to know about the return statement just to write python on a daily basis right but there's all these subtle things that can kind of occur so it's just really good to see something like this that really breaks it down explains it very clearly and is not you know unafraid to go into that level of depth and the best practices section is one of my favorite parts of it and that was i was just really glad to see because i think there is a lot of confusion yeah definitely and a lot of you just don't know like when you're when you're self-taught like should i like like this whole thing of like should i return a value like none explicitly or not like does it matter right and no one really answers that for you should i write big complex expressions you know even if it's like a one-liner or should i split it up i mean there's all these things and you might not be thinking about like you know how easy is it to debug that so i'm really happy that this is out there and on real python and i hope that people read it and really get a lot out of it i think they will yeah so what's your next one uh my next one is kind of fun it's uh it's called python for kids and it's a it's a github repo that is a collection of basically links to articles that this this guy kevin thomas has written on linkedin okay and the repo contains code samples and things like that that that complement the the articles that he wrote on linkedin and since we featured this on pi coders there's been some pretty big changes to it that i've i just saw when i brought it up today so when we featured this he had only up to i think less than 8 out of 10 lessons that he was planning and he's now completed those 10 lessons and then now started a whole new section on like an intermediate course continuing on so he's now up to lesson 14. oh nice and these are specifically targeted towards kids and he doesn't say what age it's for but i would guess that you know it's sort of that middle school high school age yeah it's all focused around python for microcontrollers so specifically specifically the bbc micro bit and i guess in the intermediate stuff he's using an expressive esp32 development board it's very hands-on and you know introducing python but also with being able to make sounds and make you know little robots and just you know stuff that kids might need to help keep their attention because it's like when you're able to type in some code and see a light go off or see something like physically move that i mean even for adults it's really exciting i don't know just that first time right like you just it's like oh man that the servo is actually you know spinning and like the wheels turning and ah this is amazing yeah so it's um yeah it's it's really cool it's the they're really well written articles and it's it's a just a really nice collection kind of everything all in one spot and like i said i'm just kind of surprised i didn't realize that there was going to be an intermediate course here so there's even now more it all uses the moo debugger or mu and not debugger uh the mu editor yeah the ide idea and very simple id which is great it is yeah mu is really nice and you know one of the one of the advantages of mu is it comes with python 3. so when you download mu it just comes with python so it simplifies the install a lot you know just like go ahead and get started and uh it also has a bunch of uh sort of built-in features to connect to these microcontrollers and and things like that so it simplifies that process as well so if you want it really is a great way to kind of just get started for a complete beginner uh because it kind of cuts out a lot of the frustrating bits that might might cause people to kind of give up prematurely on that end so yeah it's just a really fun series you know if you if you have children that are interested or if you teach programming for children this that has tons of resources for you to to use and look at so definitely check it out i was trying to think like what's the best way to get a micro bit looks like microbit.org you can get like an educational package with it and it's trying to see like you can get them from adafruit okay adafruit has them also okay cool yeah all right nice okay well that kind of moves us into the project area yeah and i have a project but it's more of you know it's kind of an article that features some hardware in it and i i think i'm more interested in the hardware yeah because i had never heard of it it was very new to me um so it's a an article on medium by adam gateghi his article's name build a face recognition system for 60 dollars with a new nvidia jetson nano 2 gigabyte and python now the 60 dollars i don't think is correct because that's the price of the board right but you know the other things you need with it you need like batteries and you need a camera and that i think the camera itself is more than that but um depending on the quality of the camera but the the board is super interesting so i didn't know nvidia made these i think they are fairly new but they are brand new it's yeah yeah well this model is right yeah the idea is if you can think of something we've talked about multiple times i think already which is a raspberry pi just you know this small i guess we call it a microcomputer what have you but it's a full functioning computer and since you can plug a keyboard mouse plug in the display it has like you know ethernet and so forth okay well what makes this different well a huge heatsink should give you an idea right away it has this gigantic heatsink that's sitting on top of it that probably weighs way more than the board itself because it has a gpu as the nvidia maxwell gpu built into it and the idea of getting gpu power should be pretty exciting for people that are interested in okay i want to do more with python and data science with this and maybe potentially do something like facial recognition or you know whatever learning or you know all those kinds of things that that require more than what say the stock processor that comes with a raspberry pi would allow things where you can do more you know scientific type of experimentations that can rely on the gpu inside of it and so this thing has come along and at 59 you know i'm super interested in getting one it's unlike pre-order and we'll definitely include links for for that and there's a whole bunch of additional projects uh that nvidia allows on it and it behaves very similar to a raspberry pi in the sense that you install an entire operating system you put it on a small sd chip one of those micro sd cards and so once you've unpacked that using your computer to the little card you insert the card right underneath that big old heatsink and it you know boots up like a little computer and then you can instruct it you can it has a version of linux on it and you can you know use a bunch of tools and that's where this article is great is that it goes through diving into not only assembling the system but just like okay what are the the kinds of commands you need to get just started and getting uh you know like a python-like environment going on it so you can kind of start playing with it and so this whole thing goes into like okay adding a raspberry pi i'm guessing that's a company i'm confused on that but they have introduced a camera module and it's been through a few revisions but they have a fairly recent new revision of it things like version two dot x or whatever that can attach to the board it's like as an accessory that's you know pretty powerful camera it's not small either it's actually kind of a large looking little device but attaching that camera to it and then you know going through this whole process of of creating it and then beyond getting the python code up and running on this device the end of the article really dives into like okay how do you make it standalone and so you might need an hdmi a little display uh you're gonna need some kind of battery power potentially or some kind of other way of remotely powering it anyway so it kind of goes through the whole walkthrough and code on github that you can kind of work with and adapt to your uses but again the nvidia site has a whole bunch of other additional projects and examples of stuff everything from you know doing audio to you know little robots they have i think called the jetbot which i think is a kit for doing a lot of the stuff with it so anyway i just didn't know about this hardware and it intrigues me and definitely if you're in the python space and you want to do even more advanced kind of ai or things that would take advantage of a gpu i would definitely look into this what kind of project you got this week so this week i've got it's not a very new project as far as i can tell it's like a year and a quarter hold me a year and a few months okay but i i just heard about it it's it's from google it's called google research football and i was you know it came up as like i think one of the trending projects uh python projects on github and so i just was like what is what is this i'll look into it it's really cool it's all about reinforcement learning and reinforcement learning is used to train like nai they can interact with like an environment it can solve complex tasks and it can work with like real world applications like robotics like self-driving cars uses that's like all reinforcement learning they've been used to do things like uh learn to play console games learn to play go chess you know all these things and you know get good enough that they can actually beat some of the best masters we have of these types of games so this is a research environment for reinforcement learning that is based on the world's most popular sport football or soccer if you're american the football where you actually use your foot that's right it's modeled after like i mean it looks like like the like the fifa game you would play on like xbox or something it's a it's a very complex 3d environment it provides this whole engine for you to use to to train agents to uh to learn to play football and it's it's just incredible i mean it's beautiful graphics yeah it looks like it uses pi game and sdl it's uh yeah it's all written in python and you know i'm looking at so there's the uh the github repo that's got a bunch of information and then at the top of their readme they've got links to things like the game server and uh a blog post that's actually got like pictures and stuff within it but yeah they're um let's take a peek in there oh do they not have a requirement maybe in their setup pi yeah they're using pygame opencv and scipy and something called gym huh i've never heard of gym before gym yeah a tool fit tool kit for oh for developing and comparing reinforcement learning algorithms there you go yeah so it's um i think it's is it 100 python what's on the side here 72.7 python 25.5 jupiter notebook so it's like basically all all python which is kind of crazy because if you look at the pictures and from the blog post i mean this is it's it's just like i've never seen a pie game a game look like that it's really good so really cool yeah so if you like soccer and are interested in uh reinforcement learning this looks like a really cool way to kind of combine those two interests and and have a lot of fun working with that nice yeah it kind of ties in a whole bunch of stuff there and then i haven't talked about gaming a whole lot in the last several months but i'm intrigued to kind of dive back into that and i have some guests in mind that hopefully i could get onto the that'd be fun on the show dive a little bit deeper into that and this would be kind of a cool project to explore yeah well that's great thanks for coming on the show and bringing all those articles again yeah thanks for having me all right talk to you soon david see ya i want to thank david amos for coming on the show this week and bringing along all those great articles and i want to thank you for listening to the real python podcast make sure you subscribe to the podcast in your favorite player and if you like the show leave us a five star rating and a review you can find show notes with links to all the topics we spoke about inside your podcast player or at realpython.com podcast and while you're there you can leave us a question or a topic idea i've been your host christopher bailey i look forward to talking to you soon
Original Description
The Python return statement is such a fundamental part of writing functions. Is it possible you missed some best practices when writing your own return statements? This week on the show, David Amos returns with another batch of PyCoder’s Weekly articles and projects. We also talk functional programming again with an article on the Python map function and processing iterables without a loop.
We cover several other articles and projects from the Python community including, interactive data visualization with Pygal, everything you need to know about namedtuples, PEP 638 syntactic macros, python for kids, the new Nvidia Jetson board, and a reinforcement learning project named football.
Topics:
- 00:00:00 -- Introduction
- 00:01:33 -- Interactive Data Visualization in Python With Pygal
- 00:06:40 -- Python's map(): Processing Iterables Without a Loop
- 00:12:51 -- Everything You Need to Know About Python's NamedTuples
- 00:19:46 -- PEP 638: Syntactic Macros
- 00:26:43 -- Video Course Spotlight
- 00:27:57 -- The Python return Statement: Usage and Best Practices
- 00:34:42 -- Python for Kids
- 00:38:03 -- Build a Face Recognition System With the Nvidia Jetson Nano
- 00:42:32 -- football: Reinforcement Learning Environment
- 00:45:51 -- Thanks and goodbye
👉 Links from the show: https://realpython.com/podcasts/rpp/31/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Real Python · Real Python · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A better Python REPL – bpython vs python interpreter
Real Python
Introducing large-type.com – A Utility Website
Real Python
Reading Hacker News Without Wasting Tons of Time
Real Python
Forward References and Python 3 Type Hints
Real Python
Using Sublime Text as your Git Editor
Real Python
Python Code Linting and Auto-Complete for Sublime Text
Real Python
Make your Python Code More Readable with Custom Exceptions
Real Python
Write Better Tests with Sublime Text's Split Layout Feature
Real Python
How to Use Sublime Text from the Command Line
Real Python
Rename Variables with Multiple Selection in Sublime Text
Real Python
Sublime Text Settings for Writing PEP 8 Python
Real Python
Write Cleaner Python with Sublime Text's Indent Guides
Real Python
Sublime Text Whitespace Settings for Python Development
Real Python
Function Argument Unpacking in Python
Real Python
Python Code Review: Debugging and Refactoring "Conway's Game of Life" + Automated Tests
Real Python
Using "get()" to Return a Default Value from a Python Dict
Real Python
A Python Shorthand for Swapping Two Variables
Real Python
Python Code Review: Refactoring a Web Scraper, PEP 8 Style Guide Compliance, requirements.txt
Real Python
Click & Jump to Test Failures from the Command Line (iTerm2)
Real Python
Setting up Sublime Text for Python Developers
Real Python
Sublime Text + Python Guide Overview
Real Python
Python Code Review: Adding Pytest Tests to an Existing Python Web Scraper
Real Python
Type-Checking Python Programs With Type Hints and mypy
Real Python
A Shorthand for Merging Dictionaries in Python 3.5+
Real Python
Python Code Review Flask Web Security Tutorial + Virtualenvs, requirements.txt
Real Python
My Python Code Looks Ugly and Confusing – Help!
Real Python
Setting Up a Programmer Portfolio/Developer Blog – How To Get Started
Real Python
Do I Need a GitHub/GitLab/Bitbucket Profile as a Developer?
Real Python
Programmer Portfolio – Example and Walkthrough
Real Python
How to Get Your 1st Speaking Gig at a Tech Conference
Real Python
How to Build Your Public Speaking Skills as a Developer
Real Python
The Object-oriented Version of "Spaghetti Code" is "Lasagna Code" ?!
Real Python
Setting up Sublime Text for Python Developers – Lesson #1
Real Python
Cool New Features in Python 3.6
Real Python
"is" vs "==" in Python – What's the Difference? (And When to Use Each)
Real Python
Emulating switch/case Statements in Python with Dictionaries
Real Python
Python Function Argument Unpacking Tutorial (* and ** Operators)
Real Python
What Code Should I Put On My GitHub/GitLab/BitBucket Profile?
Real Python
A Crazy Python Dictionary Expression ?!
Real Python
String Conversion in Python: When to Use __repr__ vs __str__
Real Python
Method Types in Python OOP: @classmethod, @staticmethod, and Instance Methods
Real Python
Optional Arguments in Python With *args and **kwargs
Real Python
Python Context Managers and the "with" Statement (__enter__ & __exit__)
Real Python
Installing Python Packages with pip and virtualenv / venv
Real Python
"For Each" Loops in Python with enumerate() and range()
Real Python
Python Code Review: LibreOffice Automation and the Python Standard Library
Real Python
Managing Python Dependencies With Pip and Virtual Environments – Lesson #1
Real Python
Python Tutorial: List Comprehensions Step-By-Step
Real Python
Leveraging Python's Implicit "return None" Statements
Real Python
What's the meaning of underscores (_ & __) in Python variable names?
Real Python
Python Data Structures: Sets, Frozensets, and Multisets (Bags)
Real Python
Writing automated tests for Python command-line apps and scripts
Real Python
How to find great Python packages on PyPI, the Python Package Repository
Real Python
Immutable vs Mutable Objects in Python
Real Python
PyPI vs Warehouse, the Next-Generation Python Package Repository
Real Python
pep8.org — The Prettiest Way to View the PEP 8 Python Style Guide
Real Python
My Experience at PyCon 2017 in Portland
Real Python
Pylint Tutorial – How to Write Clean Python
Real Python
"Reverse a List in Python" Tutorial: Three Methods & How-to Demos
Real Python
Python Refactoring: "while True" Infinite Loops & The "input" Function
Real Python
More on: Python for Data
View skill →Related Reads
📰
📰
📰
📰
PANet Paper Walkthrough: When Feature Pyramids Go Bottom-Up
Towards Data Science
CCTV Action Recognition: Comprehensive Fine-Tuning & Real-Time Deployment Guide
Medium · Python
I built a background remover that keeps the fine hair edges
Dev.to · KunStudio
I Built a Python Package to Solve My Own CV Frustration — 7K Downloads in a Week
Medium · Machine Learning
🎓
Tutor Explanation
DeepCamp AI