This question already has answers here:
Python function overloading
(19 answers)
Closed 5 years ago.
In Python, could I write 2 methods having the same name but different number of parameters ?
em.on_create_experience(action.dest_id)
em.on_create_experience2(action.dest_id,0)
You can't write two separate methods with different parameter lists. But you can write one method with optional keyword parameters to do what you want.
Related
This question already has answers here:
How do I create variable variables?
(17 answers)
Dynamically create variable names? [duplicate]
(1 answer)
Closed 2 years ago.
For instance, lets say I have a for loop like this
for i in range(7):
var+i=i
How do I get it to work where 7 different variables are created where var0=0 and var1=1 var2=2, etc..
This question already has answers here:
String formatting in Python [duplicate]
(14 answers)
Putting a variable into a string (quote)
(4 answers)
Closed 2 years ago.
very new to python so sorry for the silly question
I've created a user input interface
fn=input()
#Where the user will input version32 for instance so effectively
fn=Version32
I've then imported a template word document using docx, which has been heavily modified based upon user input. I then want the file name output to be saved as "fn" or in this case Version32
output.save(r"C:\Users\XXX\XXX\XXX\'fn'.docx")
Where fn is a variable? Is this even possible, or am I barking up the wrong tree?
Kind Regards!!
IIUC, you can do this using f-string:
output.save(rf"C:\Users\XXX\XXX\XXX\{fn}.docx")
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__)
This question already has answers here:
What's the difference between lists and tuples?
(22 answers)
Closed 5 years ago.
In Python is it better practice to write functions that accept tuples instead of lists if the data passed should be unmodified? Similar to how passing arguments by const reference in C++ makes it clear to the user that data won't be modified.
No, tuples and lists are different things. Tuples have strucure, lists have order. Use each for its own purpose.
This question already has answers here:
Spark Equivalent of IF Then ELSE
(4 answers)
Closed 5 years ago.
I am trying to use a "chained when" function.
In other words, I'd like to get more than two outputs.
I tried using the same logic of the concatenate IF function in Excel:
df.withColumn("device_id", when(col("device")=="desktop",1)).otherwise(when(col("device")=="mobile",2)).otherwise(null))
But that doesn't work since I can't put a tuple into the "otherwise" function.
Have you tried:
from pyspark.sql import functions as F
df.withColumn('device_id', F.when(col('device')=='desktop', 1).when(col('device')=='mobile', 2).otherwise(None))
Note that when chaining when functions you do not need to wrap the successive calls in an otherwise function.