Тёмный

Python's Lambdas Explained In LESS Than 60 Seconds 

Подписаться
Просмотров 42 тыс.
% 2 930

Python's lambdas explained is less than 60 seconds. #Python #Code #Shorts

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

 

24 фев 2023

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 29   
@xzex2609
@xzex2609 Год назад
Lambdas should not be assigned to a variable.(according to pep-8) Instead, they should be defined as functions. The primary reason for this is debugging. Lambdas show as in tracebacks, where functions will display the function's name.
@Indently
@Indently Год назад
The docs say that, that’s true. But you’ll see them in a lot of professional projects being used with names regardless
@mr.g937
@mr.g937 Год назад
​@@Indently...not if these "professional" projects use a linter
@DrDeuteron
@DrDeuteron 11 месяцев назад
the danger of non-inline lambda is their scope. Here's an example of a function for a y=mx+b function maker: >>>def make_y(b): return lambda x: m*x + b >>>y1 = make_y(0) >>>y1(10) NameError... >>>m = 1 >>>y1(10) 10 >>>m = 10 >>>y1(5) 50 So the global scope variable "m" reaches into make_y and changes the output of the lambda. Fun! Could be clever, even, but super dangerous in a massive codebase.
@xzex2609
@xzex2609 11 месяцев назад
@@DrDeuteron this is not related to using lambdas or their scope , this is just a bad code , first of all you did not encapsulate your parameter in the scope of your function and the rule is it will look for the parameter at global scope . what you did was that make an y = mx + 0 = mx , and the parameter m neither is inside the function nor passed to function . so if you call the function , if there is a m in the global scope it will use it , and if it was not provided it will give you an error . there is no danger related to lambda function's here. you just simply must understand the scope of a variable in a function and classes , and write your code based on the rules. and keep in mind to encapsulate the data as much as possible . remember methods in a class has access to other functions (methods) variable inside the class . but in functional programing we do not have these access between different function and if we want to share them one way is to declare them as Global , or implement those methods inside a class .
@xzex2609
@xzex2609 Год назад
they are nameless functions and they should not be assigned a name , in this case def add(a,b):return a+b is beter (pep 8)
@DrDeuteron
@DrDeuteron Год назад
True. But from operator import add works too
@xzex2609
@xzex2609 Год назад
@@DrDeuteron the function add or x is not the case here , according to pep 8 you should not name a lambda function it is a bad practice , although it will work but pep 8 suggest that we use lambda for anonymous functions mostly for hand made call back functions . but regardless of that when we learn lambda we use this bad practice for the purpose of understanding the lambda functionality, but the teacher must say that we better not name them and use it as call backs like sorted( list , key = lambda data : data[0] > 0)
@superspies32
@superspies32 Год назад
I learned a true potential of lambda when working with Tkinter
@I_do_not_want_to_reply
@I_do_not_want_to_reply 11 месяцев назад
So true
@adventuresofavalon2477
@adventuresofavalon2477 Год назад
You can directly use len as key
@Indently
@Indently Год назад
yup
@PabloVillarVega
@PabloVillarVega Год назад
Exactly, key only expect a function that returns something
@littlenerd6185
@littlenerd6185 Год назад
@@Indently can make video explaining how can we create a small python environment in our project so that the client doesn't have to install python in their device pls :) that would be really helpful
@jefferylegere
@jefferylegere Год назад
still no frickin idea what a lambda is.
@anthonyk5645
@anthonyk5645 Год назад
lambda parameter (space available to put arguments): expression (e.g.: math formula, hence calculator). I'm beginner, it would confuse beginners when different terminology is used.
@sammyiyi7136
@sammyiyi7136 11 месяцев назад
When u assign a lambda to a variable, the variable contains the address to the lambda function object which is callable with the lambda's parameter list!
@PhilippLackner
@PhilippLackner Год назад
Instead of add(5, 10), you can also just do 5 + 10
@Indently
@Indently Год назад
Why did I never think of this before!
@bloxfruit_player788
@bloxfruit_player788 Год назад
The only thing needed to understand whats lambda is to need k ot its, using for creating func, but if u ront want to create func u just create lambda, and its anonymous func
@RexVelde
@RexVelde Год назад
Code golfing without lamba is like treasurehunting without map. I need them both.
@gamingland2074
@gamingland2074 11 месяцев назад
i still can't understand the point of using it
@Fine_Mouche
@Fine_Mouche Год назад
Why sorted don't sorted the words alone ? what it need a lambda function in it ?
@olivierdulac
@olivierdulac 4 месяца назад
The lambda [or function] specifies the way you want to sort it by [lexicographically? by size? by amount of vowels? etc]
@gramps572
@gramps572 Год назад
So, it's like an arrow function ? Or is there any difference?
@xzex2609
@xzex2609 Год назад
JavaScript arrow functions are roughly the equivalent of lambda functions in python or blocks in Ruby, but they have more intricate details. Arrow functions allow a developer to accomplish the same result with fewer lines of code and approximately half the typing. But in python it has lots of use cases. they are incredible.
@realcontentgamer
@realcontentgamer Год назад
POV: you were tired of changing the file name to py + current month (pyjan, pyfeb, pydec) so you put it at indently
@Indently
@Indently Год назад
You should seriously look into a career as a detective, that sums it up pretty well
@realcontentgamer
@realcontentgamer Год назад
@@Indently I actually want a career as a programmer