On 28/03/16 20:31, Abhishek Kumar wrote: > I am a computer science student,I want to know how python keeps track of > data types of variables and is there a way to get data types of variables > without actually running the script(with type() function).
First, recall that Python variables are just names so they don't really have a type as such, it's the objects that they reference that have types. Second, I'm not sure what you mean by "get data types of variables without actually running the script"? The variables/objects don't exist until you run the script through the interpreter. Python is not statically typed so you cannot analyse the source code in any meaningful way to get the information. Can you explain what you want to do with a short example. For example if I have a script hello.py like ################## import sys message = sys.version greeting = "Hello there, " print greeting + message message = 42 print "the answer is ", message ################## Now how would you want to examine that to determine the "types" of sys, message and greeting?? Especially since message takes on two types(objects) during the execution of the code. > AST can help, but am unable to figure out.Please help me. I don't see how AST would help in this case. It looks at the syntax and in Python there are no type declarations in the syntax. (The new type hints feature might help but very few people actually use that and it doesn't apply in all scenarios) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor