This question already has answers here:
how to tell a variable is iterable but not a string
(11 answers)
Type checking: an iterable type that is not a string
(1 answer)
Closed 6 years ago.
I have a function designed for interactive usage that takes either a single "scalar" value as argument or an iterable. The output should match the input. Here is a toy example.
def func(x):
flag = False
if isinstance(x, int):
flag = True
x = (x,)
retval = tuple(i**2 for i in x)
return retval[0] if flag else retval
>>> func(5)
25
>>> func((1,2,3,4))
(1, 4, 9, 16)
Question: Is there a more convenient method to check whether the user provided a "scalar" or an iterable? I don't want to rely on checking the with isinstance?
I tried with hasattr(x, '__iter__'), but this return True for "scalar" string input (not covered in the example here).
Related
This question already has answers here:
Determine the type of an object? [duplicate]
(15 answers)
Closed 7 months ago.
I am trying to make my own function that receives an int, a list, or a tuple. The function returns true if it is a list else a false if it not a list. But I keep getting none or false when I try to run it.
def int_list_tuple(value):
if value==list:
return True
if value !=list:
return False
print(int_list_tuple(6))
print(int_list_tuple([6, 7, 9]))
print(int_list_tuple((6, 2)))
There is already a function for that
isinstance(obj, list)
If you want to do it this way, which isn't recommended at all for good programming practices... (You should really be asking yourself why you need to do this.)
def list_check(value):
if isinstance(value, list):
return True
return False
print(list_check(6))
print(list_check([6, 7, 9]))
print(list_check((6, 2)))
Another not-as-durable way of doing this is like in the linked duplicate question.
print(type(6) is list)
print(type([6, 7, 9]) is list)
print(type ((6, 2)) is list)
The problem is that if you are needing to check for type, while sometimes unavoidable, should be avoided.
This question already has answers here:
Logical operators in Python [duplicate]
(4 answers)
Closed 2 years ago.
I though I understood Python the "and" operator. But after seeing
assert (None and None) is None
it was apparently that my understanding was not precise. Which was that None and None would be the same as bool(None) and bool(None).
Does anybody have definition of the "and" operator and can explain the logic.
According to the official documentation:
help('and')
[...] The expression "x and y" first evaluates x; if x is false,
its value is returned; otherwise, y is evaluated and the resulting
value is returned. [...]
and returns the first value if it is "falsey"*. Otherwise it returns the second one.
For example:
3 and 6 -> 6
0 and 7 -> 0
[] and 'abc' -> []
* a thing is falsey if bool(thing) is False
This question already has answers here:
How to test the membership of multiple values in a list
(12 answers)
Closed 3 years ago.
I have an if statement which always returns as true even if it is not.
I have a global list :
NUMBERS_LIST = []
Numbers get added to it via an API call, that works fine.
When it does the following:
def func():
if 8 or 9 in NUMBER_LIST:
return true
elif 1 or 2 in NUMBER_LIST:
return true
But for some reason, it always returns true on the first if statement, even if NUMBER_LIST = [1]
I debugged my program and can see that NUMBER_LIST does contain 1, it's type is int.
I tried doing int(8), converting both types to str but that did not fix my issue.
When I debugged and step through the program, it doesn't really tell me much, I am using Pycharm.
or does not distribute. What you have written is not equivalent to
if 8 in NUMBER_LIST or 9 in NUMBER_LIST:
which is what you want, but to
if 8 or (9 in NUMBER_LIST):
Since 8 is a truthy value, the containment operation is never evaluated.
This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 4 years ago.
I had trouble understanding why this function returns None:
def rem(x, a):
"""
x: a non-negative integer argument
a: a positive integer argument
returns: integer, the remainder when x is divided by a.
"""
if x == a:
return 0
elif x < a:
return x
else:
rem(x-a, a)
rem(7, 5)
I then realized that the last line in the function should be:
return rem(x-a, a)
I am still unsure as to what really happens in the first one. It looks like there are 2 seperate function calls, one returning 2 and the other None... any help?
All python function returns None if it doesn't terminate by a return statement. In your case, you successfully called the recursion but you discarded the result in the else part. And after the if-block, the default return None is implicitly executed.
This question already has answers here:
What's the canonical way to check for type in Python?
(15 answers)
Closed 5 years ago.
Why does this print False in Python 3?
>>> 1 == int
False
Because that isn't at all doing what you think it is. You are comparing the integer value 1 with the type int; naturally they are not equal.
If you want to check whether an object is of a certain type, use isinstance:
isinstance(1, int)
I guess, what you want to use is this:
>>> type(1) is int
True
or
>>> type(1) == int
True