Home > Blockchain >  Convert function arguments to str
Convert function arguments to str

Time:12-29

I wrote a function and call it as below:

from lib import base_frequency

base_frequency("AB610939-AB610950.gb", "genbank")
#This calls the function that uses a BioPython code.

How could I pass the function arguments as below?

base_frequency(AB610939-AB610950.gb, genbank)

Note that quotes are missing. Should I do this? Is there a recommended nomenclature in Python when function argument is sting?

I thought this required me to convert filename and record format to a string inside the function. That is:

AB610939-AB610950.gb to "AB610939-AB610950.gb"
genbank to "genbank"

I have tried str(AB610939-AB610950.gb) inside the function but it did not do the job.

CodePudding user response:

There is no way to do this without quotes, or else Python will interpret it as an expression. Take the first argument for example,

AB610939-AB610950.gb

Python will read this as a subtraction operation between two variables, AB610939 and the gb property of AB610950, not a sequence of characters. The only way to stop this is to surround it in quotation marks to make it string literal.

CodePudding user response:

Is there a recommended nomenclature in Python when function argument is string?

Yes.

Enclose a string literal "within quotes".

Here is a pair of valid examples of doing that:

base_frequency("AB610939-AB610950.gb", "genbank")

That produces a pair of str values, pushes them onto the stack, and calls the base_frequency function.

If e.g. the 2nd arg was an unquoted genbank, that would mean something entirely different. It would mean "please look up the value of the variable named genbank and pass its value to the function."

If you ask for a variable that does not exist, the result will be a NameError.

  • Related