简体   繁体   中英

TypeError: split() takes no keyword arguments in Python 2.x

I am trying to separate a section of a document into its different components which are separated by ampersands. This is what I have:

name,function,range,w,h,k,frac,constraint = str.split(str="&", num=8)

Error:

TypeError: split() takes no keyword arguments

Can someone explain the error to me and also provide an alternate method for me to make this work?

The parameters of str.split are called sep and maxsplit :

str.split(sep="&", maxsplit=8)

But you can only use the parameter names like this in Python 3.x. In Python 2.x, you need to do:

str.split("&", 8)

which in my opinion is the best for both versions since using the names is really just redundant. str.split is a very well known tool in Python, so I doubt any Python programmers will have trouble understanding what the arguments to the method mean.

Also, you should avoid making user-defined names the same as one of the built-in names. Doing this overshadows the built-in and makes it unusable in the current scope. So, I'd pick a different name for your string besides str .

The error states that you can't provide named arguments to split . You have to call split with just the arguments - without the names of the arguments:

name,function,range,w,h,k,frac,constraint = str.split("&", 8)

split doesnt get keyword arguments str or num . Do this instead:

name,function,range,w,h,k,frac,constraint  = str.split('&', 8)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM