In Python i am creating a classmethod and i am using a classmethod to get my parameters from a string by spliting it i am getting what i want but cannot understand why i get a list when i am not using * and not getting list when using *. here's my code below.
@classmethod
def from_one_str(cls, onestr):
print(*onestr.split(",")) # getting two string
# on running this is result - John C++
print(onestr.split(",")) # getting a list of two strings
# on running this is result - ['John', 'C++']
# This is what i am running - John = Programmer.from_one_str("John,C++")
In a function call, the
*operator unpacks the given list into separate arguments.is thus equivalent to
To bring this back to your code:
from_one_str()with the string"John,C++""John,C++".split(",")into["John", "C++"]printby the*operator.printprints each argument –"John"and"C++"– separated by space.John C++.