variable-assignment

How do I 'declare' an empty bytes variable?

How do I 'declare' an empty bytes variable? Question: How do I initialize (‘declare’) an empty bytes variable in Python 3? I am trying to receive chunks of bytes, and later change that to a utf-8 string. However, I’m not sure how to initialize the initial variable that will hold the entire series of bytes. …

Total answers: 5

assigning class variable as default value to class method argument

assigning class variable as default value to class method argument Question: I would like to build a method inside a class with default values arguments taken from this class. In general I do filtering on some data. Inside my class I have a method where normally I pass vector of data. Sometimes I don’t have …

Total answers: 4

Compressing `x if x else y` statement in Python

Compressing `x if x else y` statement in Python Question: I’m quite acquainted with Python’s ternary operator approach: value = foo if something else bar My question is very simple: without prior assignments, is there anyway to reference the term being evaluated in (if …) from one of the return operands (… if or else …

Total answers: 3

Assign split values to multiple variables

Assign split values to multiple variables Question: I am currently writing a Python script to handle some logs and reformat certain parts. Part of the script makes use of the following code (as an example): var1,var2,var3=foo.split("|") Which works fine. However this perhaps doesn’t look so nice (taking away Python’s readability factor) if there are 39 …

Total answers: 5

How does Python's comma operator work during assignment?

How does Python's comma operator work during assignment? Question: I was reading the assignment statements in the Python docs ( http://docs.python.org/reference/simple_stmts.html#assignment-statements). In that it is quoted that: If the target is a target list enclosed in parentheses or in square brackets: The object must be an iterable with the same number of items as there …

Total answers: 3

What is this kind of assignment in Python called? a = b = True

What is this kind of assignment in Python called? a = b = True Question: I know about tuple unpacking but what is this assignment called where you have multiple equals signs on a single line? a la a = b = True It always trips me up a bit especially when the RHS is …

Total answers: 4

Initialize many string variables

Initialize many string variables Question: I’m initializing a lot of string variables as follows: a, b, c, d, e, f, g, h = “”, “”, “”, “”, “”, “”, “”, “” You can see that this doesn’t look very nice (morever the variables have longer names). Is there some more compact shortcut? Asked By: xralf …

Total answers: 6

Assignment with "or" in python

Assignment with "or" in python Question: Is it considered bad style to assign values to variables like this? x = “foobar” or None y = some_variable or None In the above example, x gets the value ‘foobar’. Asked By: TheOne || Source Answers: No, it’s a common practice. It’s only considered bad style for expressions …

Total answers: 4

Multiple assignment and evaluation order in Python

Multiple assignment and evaluation order in Python Question: What is the difference between the following Python expressions: # First: x,y = y,x+y # Second: x = y y = x+y First gives different results than Second. e.g., First: >>> x = 1 >>> y = 2 >>> x,y = y,x+y >>> x 2 >>> y …

Total answers: 11

Python: Assign Value if None Exists

Python: Assign Value if None Exists Question: I am a RoR programmer new to Python. I am trying to find the syntax that will allow me to set a variable to a specific value only if it wasn’t previously assigned. Basically I want: # only if var1 has not been previously assigned var1 = 4 …

Total answers: 9