I want to make it like this:
>>> myfunc("strawberry")
ok
# myfunc only works with strawberry
I know that most people will answer with:
def myfunc(something):
if something == "strawberry":
print("ok")
But I want to do all this in the parameter setting.
Like, kind of like this:
def myfunc(something: OnlyThese["strawberry", "cake"]:
print("ok")
Although the code above is very incorrect, I want to see if Python already has a feature like this.
Don't believe there is a way to do what you are wanting to do without writing code in the function body.
I found answers to a similar question at
enforce arguments to a specific list of values
Related
It probably has been asked before,
But I couldn't find something like that,
I'm trying to print a generator object:
n="12234451"
print(*[n[c]for c in range(len(n))if n[c]!=n[c-1]or c==0],sep="")
I usually use something like that, however, I'm wondering if it's the most efficient way to do it
(without changing the fact that the expression is in the print)
Thanks for your time
I am building a small API. For now I am having this workaround solution for optional arguments that need to be send to function later.
if 'A' in request.args:
A = request.args['A']
else:
A = 0
but I feel like that there must be something precise. This seems a bit unprofessional. Something like when I work argparse
parser.add_argument('--A', type=int, required=False')
Please, is it possible to shorten the first section a bit and use some of their functions for it? Thanks
You can use get method something like:
a = request.args.get('A', None)
It puts 'A' value if it exists in args or just None if it doesn't. You can also replace None with any other data like 0 or 'nothing' or everything else.
I'm amazed to not have come across any such site so far.
For example, when I'm using the Python's dict() function. I want to what are the different parameters that it can accept. Something like the following:
return: dict, dict(list)
return: dict, dict(dict)
return: dict, dict(dict + dict)
return: dict, dict(tupple of format = (element=value, element2=value2))
Everywhere I search on the Internet it just brings me to limited examples rather than showing me a signature.
Here on Stackoverflow.com, I came across a question that stated we cannot use dict(dict). That dict() function can only be used with a list.
Is there any site/link that shows most of the ways that dict can be used or the signature of dict in the above format?
Follow the official documentation of Python:
https://docs.python.org/3.6/
You will get most of the ideas from it.
I want to create a function that takes some chain of characters as an argument, and uses it as a str object.
def useless_function(argument) :
print(argument)
useless_function(banana)
--> NameError: name 'banana' is not defined
So this is what I did : I created a decorator that turns whatever I enter as argument into a str my function can print.
def decorator(f) :
def wrapper(arg_f) :
str_arg = str(arg)
f(str_arg)
return wrapper
So now I can decorate useless_function with my decorator, and useless_function(banana) will print 'banana'. And it will work with whatever it enter as an argument of useless_function.
My question is : is there a more elegant way or a simpler and faster way to do this automatic transformation into a string that can be used as an argument ?
Can you please elaborate because I don't understand what it is that you are looking for or saying.
If you mean: inside a function can you do input("variable")? Then the answer is yes. It is just essentially raw_input() from python2. The input from your keyboard will always be a str if I am not mistaken.
Update after edited post:
It is still not any more clear what you are trying to do.
At the end of the function, you do return * but I assume you know this.
I am really confused, but have you considered just doing str(argument)? As in takes_argument(str(argument))
2nd Update after 2nd edit:
I think I finally understand what you are trying to do, but I might be wrong.
Now, the problem is that def useless_function(argument) : will expect argument to be defined as a variable with some value(s). I am not aware of any other way than actually putting "argument" to tell python that what you are inserting is a string of characters rather than trying to reference some variable and its value. It is the same case as with print('something'), if I were to put print(something), python would try to look up the variable called something which you haven't defined.
Hope that makes sense.
I have a python module which has several methods:
module.py
def a():
return "This is a method"
I want to add some additional functionality to method a by calling it from script to avoid modification in module itself.
How can I add additional operations or arguments to that module calling it from script?
For example I imported module.py in my script and in that script I add two arguments to method "a" and addition of those arguments in addition to:
return "This is a method"
Well, you cannot really change any imported methods as far as I am aware. That does not mean it is completely impossible, I just wouldn't know how.
Question is though if that is actually what you want (as I believe it is a very uncommon way of handling code and I am guessing not an advisable one)?
Could it be that what you actually want to achieve is something more like this:
module_script.py
def a(variable):
return_value = "This is a " + str(variable) + " method!"
return return_value
second_script.py
import module_script
print(module_script.a("nice"))
The output of the second script would be:
"This is a nice method!"