Jump to content

break this down for me please

Ripred

Alright, i already know it just works this way but i need it explained to me like I'm a child because i feel like my brain is caught in a loop each time I do this, okay so, dictionaries in python, my understanding is as such

 

something = {key : Value,

}

 

okay i got that the first item is the key, and the next item to the right of the colon is the value, then the comma is the end or break separating from the next pair, i got this, what's got me puzzled is adding in functions, now I know this just works, I'm just trying to understand it better, so here's my example

operator_list ={"+": lambda x,y: x+y,
                "-": lambda x,y: x-y,

I know this works being typed like this but what's causing my brain to be triggered is the comma after x, I consciously know everything to the right of the first colon is all considered part of the same value but that bloody comma has the back of my mind going, "wait hold on, wouldn't that mean y: x+y is a new pair?"

                          Ryzen 5800X3D(Because who doesn't like a phat stack of cache?) GPU - 7700Xt

                                                           X470 Strix f gaming, 32GB Corsair vengeance, WD Blue 500GB NVME-WD Blue2TB HDD, 700watts EVGA Br

 ~Extra L3 cache is exciting, every time you load up a new game or program you never know what your going to get, will it perform like a 5700x or are we beating the 14900k today? 😅~

Link to comment
Share on other sites

Link to post
Share on other sites

I assume that once python reads the "lambda" keyword, it starts searching for the semicolon that would be associated with the lambda function and so anything in between the keyword "lambda" and the semicolon (and immediately afterwards) is considered part of the lambda function even if this is inside a dictionary.

 

So Python would basically read it as

operator_list ={"+": (lambda x,y: x+y)}

 

A lambda function can take multiple arguments so it makes sense that you would be able to use commas.

 

The same can be seen if you had a dict like this

my_dict ={"key": print("hello, world")}

Once Python reads the "print" keyword it will start searching for the end of the print statement and any commas used inside the print statement stay part of the print statement instead of causing the dictionary to start a new key value pair.

Link to comment
Share on other sites

Link to post
Share on other sites

21 minutes ago, Ripred said:

I know this works being typed like this but what's causing my brain to be triggered is the comma after x, I consciously know everything to the right of the first colon is all considered part of the same value but that bloody comma has the back of my mind going, "wait hold on, wouldn't that mean y: x+y is a new pair?"

This I think is actually a really good question...it's also why I don't particularly like python syntax.

 

The general idea of how I would explain what is happening is that it's "what makes sense" in regards to the syntax you would expect.

 

So lets just break this down a bit by analyzing only the bit in question,

lambda x,y: x+y

 

now consider

lambda x

 

the above doesn't really make sense.  The lambda needs essentially what it's defined as.  It's like writing f(x) instead of f(x,y) = x + y.  It's incomplete.

 

So to an extent when python reads through it, it sees lambda and expecting the pattern

lambda [parameters]: [formula]

So at that stage until it reaches something that fits the above pattern it will keep reading and assume everything belongs to the lambda portion.

 

Similar to if you wrote something like

(x + y, z)

The reason it doesn't add (x+y then z) to the dictionary is because as soon as it sees ( it's expecting there to be an ) and until it sees that it assumes everything belongs to the ()

 

The only difference is instead of () it's beginning with lambda as though it's the ( and it's looking for the first valid formula that lambda returns to be a ).

 

Then again, I don't know Python well enough to root through the specification to see if this is an ambiguous case (one that's not defined)

3735928559 - Beware of the dead beef

Link to comment
Share on other sites

Link to post
Share on other sites

It's essentially the same as operator precedence, the "lambda" keyword takes precedence in how things are grouped, just like "1*2+5/4" has a defined result even without explicit parentheses.

F@H
Desktop: i9-13900K, ASUS Z790-E, 64GB DDR5-6000 CL36, RTX3080, 2TB MP600 Pro XT, 2TB SX8200Pro, 2x16TB Ironwolf RAID0, Corsair HX1200, Antec Vortex 360 AIO, Thermaltake Versa H25 TG, Samsung 4K curved 49" TV, 23" secondary, Mountain Everest Max

Mobile SFF rig: i9-9900K, Noctua NH-L9i, Asrock Z390 Phantom ITX-AC, 32GB, GTX1070, 2x1TB SX8200Pro RAID0, 2x5TB 2.5" HDD RAID0, Athena 500W Flex (Noctua fan), Custom 4.7l 3D printed case

 

Asus Zenbook UM325UA, Ryzen 7 5700u, 16GB, 1TB, OLED

 

GPD Win 2

Link to comment
Share on other sites

Link to post
Share on other sites

That comma in between x and y doesn't get interpreted as a dictionary entry separator because it's part of the lambda expression.

 

The interpreter is smarter than just looking for the first comma when parsing something like this. Instead, the lambda is a sub-expression where any special syntax like the commas are treated as being part of the lambda's syntax instead of the dictionary's. Only when it finishes parsing the lambda will a comma be treated as part of the dictionary's syntax.

Computer engineering grad student, cybersecurity researcher, and hobbyist embedded systems developer

 

Daily Driver:

CPU: Ryzen 7 4800H | GPU: RTX 2060 | RAM: 16GB DDR4 3200MHz C16

 

Gaming PC:

CPU: Ryzen 5 5600X | GPU: EVGA RTX 2080Ti | RAM: 32GB DDR4 3200MHz C16

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Kilrah said:

It's essentially the same as operator precedence, the "lambda" keyword takes precedence in how things are grouped, just like "1*2+5/4" has a defined result even without explicit parentheses.

 

34 minutes ago, dcgreen2k said:

That comma in between x and y doesn't get interpreted as a dictionary entry separator because it's part of the lambda expression.

 

The interpreter is smarter than just looking for the first comma when parsing something like this. Instead, the lambda is a sub-expression where any special syntax like the commas are treated as being part of the lambda's syntax instead of the dictionary's. Only when it finishes parsing the lambda will a comma be treated as part of the dictionary's syntax.

 

Thanks for the responses, I was actually thinking along these lines but when I asked Chat GPT it disagreed...which is why i came here for some clarity

                          Ryzen 5800X3D(Because who doesn't like a phat stack of cache?) GPU - 7700Xt

                                                           X470 Strix f gaming, 32GB Corsair vengeance, WD Blue 500GB NVME-WD Blue2TB HDD, 700watts EVGA Br

 ~Extra L3 cache is exciting, every time you load up a new game or program you never know what your going to get, will it perform like a 5700x or are we beating the 14900k today? 😅~

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, CogadhTaibhse said:

I assume that once python reads the "lambda" keyword, it starts searching for the semicolon that would be associated with the lambda function and so anything in between the keyword "lambda" and the semicolon (and immediately afterwards) is considered part of the lambda function even if this is inside a dictionary.

 

So Python would basically read it as

operator_list ={"+": (lambda x,y: x+y)}

 

A lambda function can take multiple arguments so it makes sense that you would be able to use commas.

 

The same can be seen if you had a dict like this

my_dict ={"key": print("hello, world")}

Once Python reads the "print" keyword it will start searching for the end of the print statement and any commas used inside the print statement stay part of the print statement instead of causing the dictionary to start a new key value pair.

I like this train of thought, picturing it like this, i can properly visualize the whole thing working, thanks

                          Ryzen 5800X3D(Because who doesn't like a phat stack of cache?) GPU - 7700Xt

                                                           X470 Strix f gaming, 32GB Corsair vengeance, WD Blue 500GB NVME-WD Blue2TB HDD, 700watts EVGA Br

 ~Extra L3 cache is exciting, every time you load up a new game or program you never know what your going to get, will it perform like a 5700x or are we beating the 14900k today? 😅~

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×