I have a Python class which needs to accept one of two mutually exclusive arguments. If the arguments are not exclusive, (ie: if both or neither are given), an error should be raised. In most scenarios, the best option would be to make two separate classes. However, I am working with an external API which requires these two attributes to
Tag: arguments
TypeError: generatecode() takes 0 positional arguments but 1 was given
I have the code below: Then, I got the error below: TypeError: generatecode() takes 0 positional arguments but 1 was given So, how can I solve the error? Answer When you call a method on a class (such as generatecode() in this case), Python automatically passes self as the first argument to the function. So when you call self.my_func(), it’s
Python function that takes one compulsory argument from two choices
I have a function: However, my intention is for the function to take only one argument (either title OR pageid). For example, delete(title=”MyPage”) or delete(pageid=53342) are valid, while delete(title=”MyPage”, pageid=53342), is not. Zero arguments can not be passed. How would I go about doing this? Answer There is no Python syntax that’ll let you define exclusive-or arguments, no. You’d have
Disable/Remove argument in argparse
Is it possible to remove or disable an argument in argparse, such that it does not show in the help? How? It is easy to add new arguments: And I know you can override arguments with a new definition by specifying the “resolve” conflict handler: But this still includes arg1 in the help message and results of parse_args Is there
How to pass arguments from vimscript functions to python interface?
For example, processing positional arguments: or the … list: Is there anyway I can do this? Answer You can retrieve the function arguments just like any other Vimscript expression through vim.eval():
Python :TypeError: this constructor takes no arguments
When the user enters an email address, and the program reads the email and display it according to its criteria (e.g yeo.myy@edu.co), like criteria: username is yeo.myy domain is edu.co I know its something to do with the “@”. this is the code this is the error I got: Answer Did you mean __init__? You’re also missing a couple selfs
format strings and named arguments in Python
Case 1: It will give KeyError: ‘arg1′ because I didn’t pass the named arguments. Case 2: Now it will work properly because I passed the named arguments. And it prints ’10 20’ Case 3: And, If I pass wrong name it will show KeyError: ‘arg1’ But, Case 4: If I pass the named arguments in wrong order It works… and
How to pass arguments to a Button command in Tkinter?
Suppose I have the following Button made with Tkinter in Python: The method action is called when I press the button, but what if I wanted to pass some arguments to the method action? I have tried with the following code: This just invokes the method immediately, and pressing the button does nothing. See Python Argument Binders for standard techniques
Getting HTTP GET arguments in Python
I’m trying to run an Icecast stream using a simple Python script to pick a random song from the list of songs on the server. I’m looking to add a voting/request interface, and my host allows use of python to serve webpages through CGI. However, I’m getting hung up on just how to get the GET arguments supplied by the
How to pass elements of a list as arguments to a function?
I’m building a simple interpreter in python and I’m having trouble handling differing numbers of arguments to my functions. My current method is to get a list of the commands/arguments as follows. Then to execute com, I check to see if it is in my dictionary of command-> code mappings and if it is I call the function I have