THIS Function TRICK In Python CHANGED My Life 😳

preview_player
Показать описание
This function trick in #Python changed my life. #Shorts
Рекомендации по теме
Комментарии
Автор

- When did you become an expert in programming?
- Last night

Zyx
Автор

You also have ** operator who pack all 'variable=value' into a dict.

Migoyan
Автор

You can also use * to spread arguments.

For example:

def f(a, b, c):
return (a + b) * c

t = (1, 2, 3)

Then:
f(*t)

sets a = 1, b = 2, and c = 3, and returns 9 as you expect.

You can also use lists instead of tuples for this.

vorpal
Автор

Where were you 12 hours ago? I learned this for a small project for work. Haha.

tannermckamey
Автор

You also should write *args and **kwargs(keywordargs)

dn
Автор

Also if you have a list you can pass it as arguments such as
Foo(*[a, b, c])

So if we have
Def foo(eggs, and, spam):

Eggs = a
And = b
Spam = c

_sleepy-
Автор

I've been wondering about the real functionality of this... I mean... Woudn't it be the same if you put all the values you want to be added into a tuple or a list, and then make a function which recieves that list of numbers and operates it? How is this functional? In which cases I mean. I'm kinda new to the programming world and I've not ended to understand this...

fonkat
Автор

In Lua this would be the ... operator. For example, function(...) local args = table.pack(...)

ArchaicVirusStudios
Автор

"amount" is for uncountable quantities. The correct English is "an unlimited number of arguments".

DrDeuteron
Автор

Could you put a tuple or list as the argument to achieve the same effect? This does have the bonus of being simple, though.

PL
Автор

I am confused on why this is necessary. You don't have to specify a length for tuples, lists, dicts when using them as arguments. If you were to remove the * it doesn't seem like function changes at all.

wait I see the difference as you aren't inputting a tuple. I just don't see the point of leaving that input ambiguous since you get a tuple as a result anyways.

extraordinaaron
Автор

It's basically like varargs in Java it seems

k_gold
Автор

This is great and all but tbh I can’t think of a situation where this would be more useful than passing in a list. Not in terms of readability, but I just can’t imagine where the parameters would come from if they weren’t already in some container

phoneman_btw
Автор

Easy way to implement method overloading

burahaunuddin
Автор

That's vardiadic functions in C, if I recall correctly. Also ...argname can be used in Js/Ts iirc to achieve similar behaviour.
Except you have to use starg.h's stuff in C.

ranchu
Автор

is not this something 'basics' of python?

_Caose
Автор

Got turned off by the clickbait title "Changed my life" (just saying, the algo likes it but the humans do not)

trudyandgeorge
Автор

This isn’t a trick but basic knowledge.

keagan
Автор

This "trick" is what every person with basic Python knowledge does every day.

ntlake
Автор

i use JavaScript, we have ...array so it is easy

StarsOfMinecrafttr