Python : String value assigned via AND & OR [duplicate] - python

This question already has answers here:
How do "and" and "or" act with non-boolean values?
(8 answers)
Using "and" and "or" operator with Python strings
(6 answers)
Closed 2 years ago.
I am modifying some Python code that is used in an ODOO Addon.
How does the logic for the following statement work?
origin = (move.group_id and move.group_id.name or (move.origin or move.picking_id.name or "/"))
Values
move.group_id is an object
move.group_id.name is a string value
move.origin is a string value
move.picking_id.name is a string
the result is "origin" is assigned the string value of "move.group_id.name"
What is this kind of assignment called?
(I tried to google this first but its not inline if logic and I have a lack of words for what this is called.)

Related

Explanation for string behavior ["somestring" "somestring"] being evaluated to ["somestringsomestring"] [duplicate]

This question already has answers here:
Python string literal concatenation
(1 answer)
String concatenation without '+' operator
(6 answers)
Closed 4 months ago.
I'm using python 3.10.4 right now.
I mistakenly wrote ["somestring" "somestring"] without a , separating the strings. Which evaluated to ["somestringsomestring"] without raising an error (which an error raised was my expectation).
And it seems like it was like that for previous versions.
How should I interpret the process of "string" "string" being evaluated to "stringstring"?

Explanation or documentation of how this string replace lambda group function works [duplicate]

This question already has answers here:
Function to dictate the replacements in re.sub method (Python)
(2 answers)
How to input a regex in string.replace?
(7 answers)
Closed 2 years ago.
I have this function here which I use to replace the a string like this 'ABCDe.CO' with 'ABCD-E.CO' in a pandas dataframe.
I don't understand how the group(0) part works, I can't find any documentation on it. Could someone explain to me what this function does or where I can read up on it?
(df.loc[df.country.eq('ST'), 'ticker'].str.replace('([a-z])', lambda x: '-'+x.group(0).upper()))

Python convert a string in to its actual Boolean value? [duplicate]

This question already has answers here:
What does Python's eval() do?
(12 answers)
How to evaluate python string boolean expression using eval()?
(2 answers)
What's the difference between eval, exec, and compile?
(3 answers)
Closed 3 years ago.
I have a function that returns a string that can be interpreted as a Boolean
def foo():
return "not(True and False)"
How can I return the actual Boolean value, in this case True?
You could use eval:
def foo():
return eval("not(True and False)")
Even tho it is virtually the only solution, but it is still not safe.
Check this link for why it is not safe.

Python: one variable but different values? [duplicate]

This question already has answers here:
Why does adding a trailing comma after an expression create a tuple?
(6 answers)
What does __all__ mean in Python?
(10 answers)
Closed 5 years ago.
I have a line of code from Python forbidden fruit module:
__all__ = 'curse', 'curses', 'reverse'
I know what strings are and I know what arrays and tuples are. What kind of variable is this? How can we use this and for what?
It's a tuple. If you want to find out the type of something, use the type function - e.g.
type(__all__)

While loop only working for one condition [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
How do "and" and "or" act with non-boolean values?
(8 answers)
Closed 5 years ago.
Im trying to validate the user's input so he/she only types in 'all' or 'sel' (not case sensitive). Might just be some syntax mistake im making.
choice=input('Type "all" or "sel": ')
choice=choice.lower()
while choice!=('sel' or 'all'):
choice=input('Invalid input. Try again: ')
choice=choice.lower()
This only works if I type in 'sel', but not 'all'.

Categories

Resources