Optional argument in python - python

I have a python program that I am trying to add an optional argument to.
If the user doesn't enter anything then I want that value to default to 20. But, if they do enter a value, I will use their value. I wrote that into the program like so:
optionParse= argparse.ArgumentParser(description='Change Number of Locations')
optionParse.add_argument('-n', dest='userDefSize')
args=optionParse.parse_args()
if args.n:
sizeOfList=args.userDefSize
else:
sizeOfList=20
But for some reason it keeps saying:
AttributeError: 'Namespace' object has no attribute 'n'
What would be the issue here? Is my if statement written incorrectly?

To answer your first question, add_argument takes a parameter 'default' which will do what you want. See the documentation
optionParse.add_argument('-n', dest='userDefSize', default=20)
To answer your second question, the 'dest' parameter redefines where the value passed in is stored in the arg namespace. In this case you told argparse to store the value in args.userDefSize instead of args.n, so args.n doesn't exist.

Related

Understanding python code?

I need some help understanding the start of this code:
def get_int_input(prompt=''):
I know what the int_input does but i need some help understanding the other parts of the line to finish my code.
I am assuming that by "other parts", you mean prompt=''.
prompt is a named parameter. Named parameters are given a default value whenever its function is called without passing a value to that parameter. Otherwise, it will use the value that was passed.
If you do:
>>> get_int_prompt()
Then the value of prompt (inside the function) would be an empty string ('').
However, if you do:
>>> get_int_prompt('What is your age? ')
Then the value of prompt (inside the function) would be 'What is your age? '.
Source: Python Central

It says "TypeError: Oppnadjur() takes exactly 1 argument (0 given)" why?

so this is my function, and it doesn't work.. why?
def Oppnadjur(djurfil):
djurfil = open("djur.txt", "r")
Djur = djurfil.readlines()
Djur.sort()
djurfil.close()
Djurlista=[]
You wrote that your function should receive one parameter, djurfil. However, you clearly did not mean to do that, as you proceed to not use that parameter, overwriting it with a different value. See the Python Tutorial about how to define functions.
The error message you see means that you had called your function as you had intended, with no parameters (Opnnadjur()), but that's not how you had defined it. So Python is looking for the parameter it thinks you should be passing in.
The error would be in your calling code, not the def of the function. You need to call Oppnadjur with one parameter. The error message suggests that you are calling it with zero parameters.
You define your function with one argument (djurfil), but the argument is unused in your function so you can get rid of it.
def Oppnadjur():
djurfil = open("djur.txt", "r")
Djur = djurfil.readlines()
Djur.sort()
djurfil.close()
Djurlista=[]

Calling a function from a string in Python throws error

I am trying to call a function from a string in Python as explained in
Calling a function of a module from a string with the function's name in Python.
Unfortunately, this doesn't work, and the Python interpreter throws an error:
TypeError: 'str' object is not callable
def current(self, t):
if self.iMode == None:
return self.i
else:
return getattr(self, 'iMode')(t)
The error refers to the last line. iMode has been set to sinx(t), that has been declared in the class.
Can anyone help me please?
From the error message it is obvious that your attribute was set to 'sinx(t)' (the string literal).
You should set it the function reference sinx instead, which is a callable.
However, as zhangyangu already said, in you example using getattr() is not needed. Maybe you really want to use a parameter (string reference) instead of the literal 'iMode'?
From the error, your iMode is a string. The iMode is not a method. There must be something wrong with your declaration. And in the class you can use self.iMode, no need to use getattr.
I think you may look for the function like eval.

How do I set default parameters in a method?

I want this method to update a certain value, unless passed another value, which it should update instead. Here's an example of what I want to do:
def update_t(self,t=self.t):
"If nothing is passed, then the default parameter is the attribute self.t"
t=t+1
I get "self is not defined"
This is a related question:default value of parameter as result of instance method
...but since this value is updated, I can't have it be None. I also cant have t be an object, because that would give me an error (can't add object and int). Any ideas? Thanks.
Use an object that can be resolved. Such as None.
def update_t(self, t=None):
"If nothing is passed, then the default parameter is the attribute self.t"
if t is None:
self.t += 1
else:
t += 1
Note that this may not change the value passed to it since the local name may be rebound if the object doesn't have a __iadd__() method.

In Python IDLE show completion what does -> mean?

I managed to get Show Completion to work thanks to this answer. But what does
str(object) -> string mean as a tip after typing the opening bracket of a function?
Example code:
linkText = "some text"
elms = browser.find_elements(By.PARTIAL_LINK_TEXT(linkText))
On Run gives: TypeError: 'str' object is not callable
Does it mean linkText should be a pointer to string?
How do I enter a pointer in Python?
To repeat my comments, which were really answers.
What you see is after typing "name(" is a 'Call tip', which tries to give the signature and the first line of the docstring. For many built-in functions, what you see are the first lines of the docstring, which give call signature and the function return value after '->'. str(x) returns a string.
Your example code is a different issue. The exception means that By.PARTIAL_LINK_TEXT is what its name says, a string, and not a function.

Categories

Resources