Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have multi line assignment in Python?

Example of nasty assignment using __ setitem __:

self.outer_scopes[self.children.item(i).getNodeName()][self.children.item(i).item(j).getNodeName()] = self.children.item(i).item(j).getTextContent()

Is it possible to do something like this?

self.outer_scopes[self.children.item(i).getNodeName()][self.children.item(i).item(j).getNodeName()] 
= 
self.children.item(i).item(j).getTextContent()

I mean, to split the assignment, not the strings after it with \ or whatever.

like image 448
Ericson Willians Avatar asked Jul 15 '26 09:07

Ericson Willians


2 Answers

You could use the backslash:

self.outer_scopes[self.children.item(i).getNodeName()][self.children.item(i).item(j).getNodeName()] \
= \
self.children.item(i).item(j).getTextContent()

But that's pretty poor style. In this case, I'd use intermediate variables to make the assignment easier to read, if possible.

like image 133
mipadi Avatar answered Jul 18 '26 23:07

mipadi


Use variables to shorten your expression. Would find better names, if I know more context:

item_i = self.children.item(i)
item_j = item_i.item(j)
outer_scope = self.outer_scopes[item_i.getNodeName()]
outer_scope[item_j.getNodeName()] = item_j.getTextContent()
like image 30
Daniel Avatar answered Jul 18 '26 23:07

Daniel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!