Home
Q&A
Education
Technology
Credit
General
Health
Insurance
Questions
Ask a Question
What are the differences between type() and isinstance()?
Home
Education
What are the differences between type() and...
asked
Jan 22, 2022
in
Education
by
JackTerrance
There are two codes, what are the difference between them: Using type import types if type(q) is types.DictType: do_something() if type(w) in types.StringTypes: do_something_else() Using isinstance() if isinstance(q, dict): do_something() if isinstance(w, str) or isinstance(w, unicode): do_something_else() Select the correct answer from above options
python
python-faq
oop
types
inheritance
1
Answer
0
votes
answered
Jan 22, 2022
by
JackTerrance
Best answer
You can use isinstance() : if isinstance(w, (str, unicode)): do_something_else() alternatively, you can use basestring abstract class: if isinstance(w, basestring): do_something_else() Hope This Helps, Happy Learning.
Related questions
0
votes
Q: Understanding Python super() with __init__() methods
I am a learner and just want to know the basic concept of super(). Here in the given code I want to know ... () ChildOne() ChildTwo() Select the correct answer from above options...
asked
Jan 24, 2022
in
Education
by
JackTerrance
python
python-faq
oop
super
0
votes
Q: What is the difference between @staticmethod and @classmethod?
Can someone please tell me the difference between a function decorated with @staticmethod and one with @classmethod? Select the correct answer from above options...
asked
Jan 20, 2022
in
Education
by
JackTerrance
python
oop
methods
python-decorators
0
votes
Q: What __init__ and self do on Python?
I'm learning the Python programming language and I've come across something I don't fully understand. In a method ... know very much. Select the correct answer from above options...
asked
Jan 28, 2022
in
Education
by
JackTerrance
python
oop
self
0
votes
Q: Python class inherits object
Is there any reason for a class declaration to inherit from an object? I just found some code that does this ... class code follows... Select the correct answer from above options...
asked
Jan 30, 2022
in
Education
by
JackTerrance
python
oop
object
0
votes
Q: What's the canonical way to check for type in Python?
How can I check a given object is of a given type? How can I check that a object inherit a given type? There's ... that it's a string? Select the correct answer from above options...
asked
Jan 22, 2022
in
Education
by
JackTerrance
string
python
python-faq
0
votes
Q: How to determine a Python variable's type?
I have a python variable how can I see that it's unsigned 32 bit or signed 16 bit, etc? Select the correct answer from above options...
asked
Jan 22, 2022
in
Education
by
JackTerrance
signed
unsigned
python-faq
python
0
votes
Q: Determine the type of an object?
Can someone tell me a easy method to check if a variable is a dictionary, list or anything else? I want to know ... is of which type. Select the correct answer from above options...
asked
Jan 21, 2022
in
Education
by
JackTerrance
python
object
python-faq
0
votes
Q: Difference between __str__ and __repr__?
Can someone please tell me the difference between _str_ and _repr_ in python? Select the correct answer from above options...
asked
Jan 21, 2022
in
Education
by
JackTerrance
repr
python-faq
python
str
0
votes
Q: What is the meaning of a single and a double underscore before an object name?
What is the meaning of using leading underscores before the name of an object in Python? And what's the difference ... , method, etc.? Select the correct answer from above options...
asked
Jan 24, 2022
in
Education
by
JackTerrance
python-faq
python
0
votes
Q: Find current directory and file's directory
In Python What command can I use to find these: Location of file which I am executing, and Current directory ( ... script in Python) Select the correct answer from above options...
asked
Jan 24, 2022
in
Education
by
JackTerrance
python
python-faq
directory
0
votes
Q: Random string generation with upper case letters and digits
How can I generate a string of size N. I want it to be of uppercase English letters and numbers like this: 7I2D86 ... do it in Python. Select the correct answer from above options...
asked
Jan 22, 2022
in
Education
by
JackTerrance
string
random
python-faq
python
0
votes
Q: Meaning of @classmethod and @staticmethod for beginner?
Can someone please tell me the basic difference between @classmethod and @staticmmethod, I am good with C++ so you ... we use them? Select the correct answer from above options...
asked
Jan 22, 2022
in
Education
by
JackTerrance
class-method
static-method
python
python-faq
0
votes
Q: What does ** (double star/asterisk) and * (star/asterisk) do for parameters?
What * and ** do for param2 in the following: def foo(param1, *param2): def bar(param1, **param2): Select the correct answer from above options...
asked
Jan 21, 2022
in
Education
by
JackTerrance
argument-unppacking
python
python-faq
syntax
0
votes
Q: How do you remove duplicates from a list whilst preserving order?
Is there a built-in that removes duplicates from a list in Python, whilst preserving order? I know that I can ... idiom if possible. Select the correct answer from above options...
asked
Jan 26, 2022
in
Education
by
JackTerrance
python
list
duplicate
python-faq
python-programming
0
votes
Q: Replacements for switch statement in Python?
I want to write a program which returns different values for different input,it can be easily done using switch ... some alternative? Select the correct answer from above options...
asked
Jan 24, 2022
in
Education
by
JackTerrance
switch-statement
python-faq
python
...