0

How can we know all possible type of a variable? Consider the following example,

n = int(raw_input())

if n<50:
        a = 5
elif n<100:
         a = 6.8
else:
         a = 'abc'

print type(a)

But, It will give output as either 'int' or 'float' or 'str', depending on value of n...

Can't we have all possible types of a variable? For example the above should give an output something like :

{int,float,str}

4
  • 4
    First. You need to get a better English spell-checker. Your question is hard to read. Second, Python has an infinite number of types (since new types can be defined via class.) What are you trying to learn about your code that you can't learn from reading it? Commented Jan 23, 2012 at 19:15
  • 1
    As a non-native speaker, I'm having trouble reading this question because of the host of abbreviations, typos, and superfluous punctuation. Could you you write these (for example Bt, abv) out? Thanks! Commented Jan 23, 2012 at 19:17
  • Python is a dynamic, duck-typed programming language. Generally this means that behaviors are more important than types; it also means that a variable can be any type. However, in this case, you wrote the code to generate a, so you know the type of a. There is no language-feature static-analysis to determine what values (and hence what types) a could have taken; you have to keep track of that. Commented Jan 23, 2012 at 19:18
  • 1
    What your asking can't be done and jcollado is right, a in python is not even a variable but just a "tag" to its value. This concept is really well explained in Code Like a Pythonista: Idiomatic Python. Commented Jan 23, 2012 at 20:01

4 Answers 4

5

You can't determine this at runtime since the type of the variable depends on the execution path. As far as I know, there is no static analysis tool that can do this either.

Sign up to request clarification or add additional context in comments.

Comments

5

You would have to enumerate all types known to the Python interpreter. I guess that would be possible but it would be essentially meaningless since there are so many types.

Python is a dynamically typed language and as such it does not have variables in the same sense as statically typed languages like C. In your snippet, a is simply a name which is bound to an object. That object could be of any type known to the Python interpreter.

There is a fundamental difference in mindset when programming in a dynamically typed language and asking questions like, what type is this object? should be avoided. Instead it is preferable to ask questions of the form, what operations does this object support? This approach to programming is known as duck typing.

Comments

2

I think that the bottom line here should be that variables in python don't have any type at all. Variables are just tags that reference to the object they have been assigned to. What really holds the type is the object itself.

In addition to this, given that any kind of object can be assigned to a variable, the amount of possible types for objects being referenced by a variable is the same as the amount of possible types you can create in the language which should be almost an unbound number depending on hardware contrains.

Comments

0

Can't we have all possible types of a variable?

No, because variables in Python don't have type; values do.

When you write type(a), you're not checking the type of a variable named a; you're checking the type of a thing that the name a refers to (which might happen to have other names as well).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.