6 CLEAN Tips To IMPROVE Your Python Functions

  Рет қаралды 81,784

Indently

Indently

Күн бұрын

Пікірлер: 90
@dragweb7725
@dragweb7725 8 ай бұрын
Another tip i would recommend, is do not be afraid to have function names a bit longer if it is more self-explanatory (for example, a function sorting a list a certain specific way should not be called something like "sort_list()" or "custom_sort()", give a hint of how it sorts the list exactly, like "sort_by_descending_order()")
@josephgaviota
@josephgaviota 8 ай бұрын
Agree. I always say "don't sacrifice clarity for keystrokes."
@saaofficial5415
@saaofficial5415 7 ай бұрын
How about "sort_descending()" 😊
@cerealport2726
@cerealport2726 6 ай бұрын
yes, i like this. When i started coding, I didnt; but I do now, for my own sanity, but also for anyone else using the code.
@TheSlimbee
@TheSlimbee 3 ай бұрын
Absolutely I about a quarter way through a 2D game development I have function names like "create_coin_list(num,direction,distance_appart):" you can tell what is happening by reading it, it takes longer to type but in larger code structured you need too
@Bradleybus
@Bradleybus 23 күн бұрын
sort_desc
@MechMK1
@MechMK1 Жыл бұрын
I love how in the pre-type-hint-era, many Python devs were very smug about how amazing it is to not have to specify your types. Type Hints get added, and now they're all figuring out that being somewhat strict about what types you pass around actually makes sense. It's almost as if being strict while writing makes things much easier when reading.
@catdisc5304
@catdisc5304 Жыл бұрын
At the same time, type hints in python don't do shit. They're just there for your code editor to help you debug faster. They get ignored by the interpreter, just like comments. You can define a function to return int and have it return a string and the code will run without errors. Your editor will just be angry and tell you "expected type int, returned type Str"
@VojtechMach
@VojtechMach Жыл бұрын
Without typehints, Python would not be a feasible option for large projects IMO.
@kellymoses8566
@kellymoses8566 8 ай бұрын
@@catdisc5304 No, you configure PyCharm to treat the wrong type being passed as an error.
@HansStrijker
@HansStrijker 7 ай бұрын
@@VojtechMach I don't know Voj, a large chuck of the IT world seems to disagree. Python had been used for exceptionally large projects before type hints were added. It's perfectly usable without, but they are often very convenient. Personally I certainly do not use typehints all the time, especially not in the first stages of code writing. I find them to be quite annoying while I haven't figured out the global structure of the project. However I do use them quite a lot. Though I already did so before PEP484 just by adding something like _:type arg1: int_ in the docstring. Also, as catdisc mentioned, type hints aren't enforced during code execution, so you still generally make duck typing structures to catch errors. It's all a tradeoff. I use C/C++ or Go in situations where strict and strong typing are required. I use Python when it isn't, or when development time (or ease) is preferable.
@jakedeschamps4454
@jakedeschamps4454 Жыл бұрын
Wow, I didn't even know I could specify return type and parameter type in python! I used to think that my python code looked super messy and potentially unpredictable compared to other languages. I'll definitely be making use of this!
@volbla
@volbla Жыл бұрын
It is a fairly recent addition, i think. Or at least recently integrated into the language standard.
@jakedeschamps4454
@jakedeschamps4454 Жыл бұрын
@@volbla that makes sense. I haven't used python in quite a while. My attention has mostly been on C, and also a little C# and C++.
@BeenThrDunThat
@BeenThrDunThat Жыл бұрын
Just keep in mind it is just a hint for the IDE, no type checking at runtime. Still useful for development though especially with multiple devs sharing code
@piedepew
@piedepew 2 ай бұрын
Don't you see it on leetcode?
@SP-db6sh
@SP-db6sh Жыл бұрын
Use match statements with walrus... Deadly combo for beautification of this code
@fernandomassey3140
@fernandomassey3140 8 ай бұрын
Great job on the video, it was well put together and Thorough. I like how you use functions that have real world application instead of useless functionality. Also how it has real functionality how multiple functions interact with each other. Great job!
@legr4ndk891
@legr4ndk891 7 ай бұрын
If only the world could write code as beautifully as this.😇
@davidl3383
@davidl3383 Жыл бұрын
thank you so much. Your chanel helps me a lot to improve my learning of python. Good practices, tips etc. So this morning, I changed my code to add these good practices ;) Very nice these little videos on specifics points. Congrats
@KinkyJalepeno
@KinkyJalepeno 2 ай бұрын
Love these. Have added all these to my studying project.
@ahobbo6401
@ahobbo6401 3 ай бұрын
Indently literally being the teacher I need in python
@codeGemz
@codeGemz 2 ай бұрын
Great video. Great explanation. Thank you!
@BokoMoko65
@BokoMoko65 Ай бұрын
Just a warning on the make payment example: Remember the asynchronous. If two concurrent processes call the function at the same time, they may incur in paying more than the balance would allow.
@ashutoshsamantaray6596
@ashutoshsamantaray6596 2 күн бұрын
I do like non local in nested functions, because sometimes, a function is modifying a variable while doing some recursive computations. This can be avoided by having the function return two values, the state, and the recursive result, but that, for immutable values, seems to me as a waste of space, and since we are not all functional programmers, unless the context of non local variables isn't explicitly clear then I'd advocate using it if it means less code
@WhyNotProgram
@WhyNotProgram Жыл бұрын
Good stuff! I still make that #4 mistake quite often in my python apps and pay for it later with code refactors haha
@yeahnick4260
@yeahnick4260 8 ай бұрын
6:02 SICK ! that is what i was looking for long time ! n1 thank you !
@haarissultan8866
@haarissultan8866 8 ай бұрын
In your last tip are you saying we should not use globals at all?
@ashersaipe
@ashersaipe Жыл бұрын
I've been binging your videos lol they're so great. If you haven't already, I'd love to see you make a video about generators and the "yield" keyword. I really don't understand it but I'm sure you could explain it well :)
@felicytatomaszewska
@felicytatomaszewska 7 ай бұрын
After watching these videos, I have started valuing VS Code more.
@JordanMetroidManiac
@JordanMetroidManiac Жыл бұрын
I think avoiding the global keyword is the best tip because it usually means you need to define an object which handles internal or persistent data automatically. I really cannot think of an instance where you must use the global keyword.
@Indently
@Indently Жыл бұрын
I’ve never used it off the top of my head, except when I was new to programming. But I’d love to hear input from other professionals in the field regarding it?
@VojtechMach
@VojtechMach Жыл бұрын
Probably some kind of read-once settings or config for your app could be a global variable. But I guess there is a better way to store settings. (also better than env. variables)
@hlubradio2318
@hlubradio2318 7 ай бұрын
Very nice I didn't know that's what a doc string is for
@whkoh7619
@whkoh7619 Жыл бұрын
Great intermediate level tips! Thanks
@ShivaniSingh-ce6vb
@ShivaniSingh-ce6vb Жыл бұрын
Man, you are awesome I loved this video I would like to watch more of these.
@fredflintstone8048
@fredflintstone8048 Жыл бұрын
Excellent advice. Thank you for sharing.
@PanduPoluan
@PanduPoluan Жыл бұрын
I see you using PyCharm. Man of class 👍🏼
@samueljehanno
@samueljehanno Жыл бұрын
Where can I get it ?
@arvind31459
@arvind31459 4 ай бұрын
​@@samueljehanno Walmart
@master_sergik
@master_sergik Жыл бұрын
Very useful tips, thanks 👍
@TamTam-i4q
@TamTam-i4q 8 ай бұрын
"So that you can reuse them"
@fullstackspiderman
@fullstackspiderman 8 ай бұрын
@3:40: you added type hints as float for *numbers parameter and the return type but the result was in int. Could you please explain?
@doveShampoo1111
@doveShampoo1111 Жыл бұрын
For tip 4, is it something like making the functions as atomic as possible? (I borrow the term "atomic" from databases which means the smallest components)
@anthonydesouza9983
@anthonydesouza9983 Жыл бұрын
Atomic actually refers to indivisible blocks (remember, you can’t break an atom into smaller components). An atomic block can actually be quite large as long as it’s run atomically. In terms of a database, you can use something called “transactions” for things that need to be run all together. For example, if you need to create a row for each step in a recipe, you could do that in a transactions. If for some reason the last step fails a DB constraint and can’t be inserted, the entire transaction is rolled back. So it’s “all or nothing”, and you can’t split up the transaction into smaller sub transactions. Tip 4 is actually closer to a concept called SRP, single responsibility principle. Each function should have exactly one responsibility. If you have a function with two responsibilities, you can split it into two smaller and simpler functions. SRP is part of a set of rules called SOLID, which I recommend checking out since following the rules leads to clear, maintainable code
@Indently
@Indently Жыл бұрын
A truly informative comment, thanks Anthony!
@samueljehanno
@samueljehanno Жыл бұрын
​@@anthonydesouza9983 👍
@muntadher8087
@muntadher8087 Жыл бұрын
Useful information thanks
@judithlee7989
@judithlee7989 Жыл бұрын
Can you do a tutorial on how to properly write function/module documentations? Thanks!!!
@arungumpina4644
@arungumpina4644 Ай бұрын
what is ... sometimes people use in def ?
@arthurarashbricenoheidari2858
@arthurarashbricenoheidari2858 Жыл бұрын
i loved your videos thanks a lot man
@RobinZuFALL
@RobinZuFALL Жыл бұрын
Where can i get that white python hoodie?
@kcvinu
@kcvinu Жыл бұрын
Few days ago, I needed to use a numpy function. One of the two parameters of that function is shape. I have no idea about the type of that parameter. So I refered to numpy docs. Sadly, there was no clue about that. But I got a link to source of code of that function. I felt very happy, because, I thought I can now read and understand what the shape is. But sadly, it was a parameter with default value None. And the docstring says nothing about the type or nature of that parameter. So at last, I decided to not use numpy and re arranged my code.
@wartem
@wartem Жыл бұрын
Which numpy function was it?
@kcvinu
@kcvinu Жыл бұрын
@@wartem numpy.ctypeslib.as_array(obj, shape=None)
@wartem
@wartem Жыл бұрын
@@kcvinu Idk, but the shape of an array can be defined as the number of elements in each dimension. The parameter shape in your example is an iterable (tuple probably), used like this in _ctype_ndarray: "for dim in shape[::-1]:" You don't need the second parameter, shape, if converting from a ctypes array. It's needed when converting from a ctypes POINTER.
@kcvinu
@kcvinu Жыл бұрын
@@wartem I have a ctype pointer which is pointed to an array of 9 c_ulongs. So I think I need the shape param. But without knowing what is shape, it is impossible to use that function. If it's the element count, then I have a value of 9. If it's the dimension of the array, then I have a value of 1. That's why I said, the docstring sucks.
@jonathanlloyd8688
@jonathanlloyd8688 Жыл бұрын
Hi, I am using vs code and when I type hint my return type some of my keywords turn white. I am using vs code. Can anyone help me out here?
@littledonkey4925
@littledonkey4925 Жыл бұрын
Hi, nice KZbin channel. I have one question: 3:24 - why didn't the interpreter yell about returning integers, when you have "-> float" next to the function?
@volbla
@volbla Жыл бұрын
That's actually an implicit exception. It is so common to want to accept both ints and floats, so it was decided that any "float" type hint will also accept ints. It is very easy to write union types these days, e.g. `int | float`, but this shorthand is still a thing.
@fluffykitties9020
@fluffykitties9020 Жыл бұрын
hi. for function return notation (point 1) why not just look at the return statement or add a comment to have notation of return type? are there any other advantages to this style of notation that make up for (visually) turning code into garbally gouk?
@volbla
@volbla Жыл бұрын
It lets your code editor know your intentions so it can automatically verify that all return statements and function calls are of the specified type. That's easier than manually checking comments every time you update or call a function.
@dragweb7725
@dragweb7725 8 ай бұрын
For your first proposition, in a function returning a variable for example, it can be unclear what type the variable is just by looking at it, and then you're forced to follow the entire function algorithm to get it. About the comment, it is better than nothing, but the Type Hints get reported in docstring description that shows to you when passing over the function call, comments do not, so as a user of someone else's function you would be forced to go read explicitly the function again to see the comments.
@reh.4919
@reh.4919 3 ай бұрын
How does he draw that right arrow? I tried dash dash right caret.
@igorthelight
@igorthelight 2 ай бұрын
On English QWERTY keyboard press "minus" then Shift + ">"
@behnamheydarian9569
@behnamheydarian9569 Жыл бұрын
I just watched this video, and after tip num 6, i have questions. If we import modules outside of the function and using it in our function, then when we use the function in another file, do we get an error because we didn't import a module, or it will import automatically?
@dragweb7725
@dragweb7725 8 ай бұрын
Yes, if the function is in another file, you have to import the module in the file where the function is. However, if you do not use the module directly in your main file, you can just import your function from the other file and use it without re-importing the module
@yapnog603
@yapnog603 Жыл бұрын
How do you do that arrow head after the def func?
@KevinArellano
@KevinArellano Жыл бұрын
It's on pycharm.
@constantinschultz7253
@constantinschultz7253 Жыл бұрын
It is just a dash "-" and a larger than ">"
@brainsmoothheadempty
@brainsmoothheadempty Жыл бұрын
It's a ligature. It automatically replaces sets of characters if you have ligatures enabled in your editor settings. In this case, it's the ligatures of the default JetBrains Mono font in PyCharm.
@L0V3V4MP1R3
@L0V3V4MP1R3 Жыл бұрын
Font ligatures
@ThisRussellBrand
@ThisRussellBrand 10 ай бұрын
in number 3, why is it float rather than `float[]`
@dragweb7725
@dragweb7725 8 ай бұрын
a float is not a container type, it cannot contain other objects of different types
@atulkumaryadav790
@atulkumaryadav790 Жыл бұрын
return type is not working for me, still I can retrun other data type without any issue
@Indently
@Indently Жыл бұрын
Yes, Python works like that
@atulkumaryadav790
@atulkumaryadav790 Жыл бұрын
@@Indently thanks, I did figured it out later. However, it's good for readability. Thanks again
@hlubradio2318
@hlubradio2318 7 ай бұрын
Nice I didn't realize print takes *args
@castlecodersltd
@castlecodersltd Жыл бұрын
Some great tips, thanks ☺. Is there a pythonic reason for all lower case function names with an under in between words? I personally don't like the underscore in function names and prefer PascalCase (uppercased first letter of each word in the function name) for names, they stand out more.
@Indently
@Indently Жыл бұрын
I wouldn’t be able to give you any history lessons on that. But that’s the proper naming convention for Python!
@larseneivind
@larseneivind Жыл бұрын
Yes, it is to see the difference between a fuction and a class.
@volbla
@volbla Жыл бұрын
Like Eivind said, it's the python convention to use snake_case for functions and variables, and PascalCase for classes. I'm sure you can find the rationale behind that in pep8, which is like the official python style guide.
@gedtoon6451
@gedtoon6451 Жыл бұрын
At 3 mins into the video you pass a number of integer parameters to a function that has a type hint of float. You did not explain that parameters with a type of float will happily accept integers as well.
@Indently
@Indently Жыл бұрын
True, but that's something you would learn in a video dedicated to type hinting.
@hlubradio2318
@hlubradio2318 7 ай бұрын
Good old Turbo Pascal
@PauloOrtolanCinemaniaco
@PauloOrtolanCinemaniaco 7 ай бұрын
So, some years ago I implemented a decorator to a project. It was pretty cool, but I neeeded to expand my system and did not have the time to make the proper change. And then I done a type variable decorator. Yeah, don't ever do this. This python system died due to main project evolution. I learned not to make this again.
@Spiderfffun
@Spiderfffun Жыл бұрын
Sometimes I need the global functions, idk why they are so bad spaghetti code
@Hdilon
@Hdilon Ай бұрын
gAY
@mzakyr342
@mzakyr342 Жыл бұрын
Hey second or third probably
@christopherknowles
@christopherknowles 18 күн бұрын
This dude definitely uses WhatsApp.
5 Good Python Habits
17:35
Indently
Рет қаралды 611 М.
Who's spending her birthday with Harley Quinn on halloween?#Harley Quinn #joker
01:00
Harley Quinn with the Joker
Рет қаралды 26 МЛН
Trick-or-Treating in a Rush. Part 2
00:37
Daniel LaBelle
Рет қаралды 46 МЛН
Hoodie gets wicked makeover! 😲
00:47
Justin Flom
Рет қаралды 127 МЛН
КОГДА К БАТЕ ПРИШЕЛ ДРУГ😂#shorts
00:59
BATEK_OFFICIAL
Рет қаралды 8 МЛН
5 Tips To Write Better Python Functions
15:59
Indently
Рет қаралды 110 М.
10 Important Python Concepts In 20 Minutes
18:49
Indently
Рет қаралды 287 М.
How To Use List Comprehension In Python
6:41
Taylor's Software
Рет қаралды 11 М.
Please Master These 10 Python Functions…
22:17
Tech With Tim
Рет қаралды 211 М.
How To Write Better Functions In Python
14:17
Indently
Рет қаралды 48 М.
25 nooby Python habits you need to ditch
9:12
mCoding
Рет қаралды 1,8 МЛН
Debugging 101: Replace print() with icecream ic()
12:36
NeuralNine
Рет қаралды 370 М.
5 Awful Python Mistakes To Avoid
22:13
Indently
Рет қаралды 27 М.
Learn Python OOP in under 20 Minutes
18:32
Indently
Рет қаралды 103 М.
Who's spending her birthday with Harley Quinn on halloween?#Harley Quinn #joker
01:00
Harley Quinn with the Joker
Рет қаралды 26 МЛН