• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What does it mean when hovering over a variable in python script on IntelliJ shows ANY?

 
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In python, if I have to know the type of a variable, I hover over it on IntelliJ and it shows string, int etc. Sometimes I have seen it shows ANY. What does that mean regarding the type of variable ?

Thanks
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It means that IntelliJ cannot determine what the type of that variable will be. Python is not a strongly-typed language.

Which is why many assignment and calling errors that Java will not permit are very easy to do in Python (or just about any of the interpreted languages).
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks

Tim Holloway wrote:It means that IntelliJ cannot determine what the type of that variable will be. .



So how to know the type in such case?



Tim Holloway wrote:
Which is why many assignment and calling errors that Java will not permit are very easy to do in Python.



What kind of assignments and calling errors are not permitted in Java ?

 
Tim Holloway
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java, if you declare a variable as int, any attempt to assign a List to it will cause a compile-time error. If you try it at runtime, you will get something like a ClassCastException.

In Python, you can easily assign conflicting types at both compile and run time. You may not discover you did until the application had been running in production for literally years.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks

In Python if I have to know the type returned by an expression or a function , I assign it to a temporary variable say temp and then hovering over it in IntelliJ would show int, string etc.

In cases where it shows ANY, how do I know the type ?
 
Tim Holloway
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:Thanks

In Python if I have to know the type returned by an expression or a function , I assign it to a temporary variable say temp and then hovering over it in IntelliJ would show int, string etc.

In cases where it shows ANY, how do I know the type ?



You don't It can be anything. Including None.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can get the actual runtime type using type(myvar). You have to run the program to see it though.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. So it can be anything because it will be know at runtime only as Python is not compiled language ?
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct. A variable that has a string value at one point in time may have an int value later. IntelliJ tries to determine what the type is, but it's not perfect.

There's actually one way to tell IDEs what the type is: type hinting. It looks like you're specifying types, but it's just a hint for IDEs. The IDE can then issue a warning or error if you try to assign something else.
 
Tim Holloway
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:Thanks. So it can be anything because it will be know at runtime only as Python is not compiled language ?



Note precisely. Especially since for performance reasons, Python does get "compiled" these days on most machines. That's what those ".pyc" files are.

It's just that interpreted languages historically have been weakly type and compiled languages historically have been more strongly typed.

Part of that history had to do with the fact that old-time computers were so much slower and simpler that your smartwatch could literally out-perform them. So building complex internal data structures to keep track of assignable data types and spending time checking them wasn't really worth it. Keeping both the compiler AND the run-time interpreter in RAM made for crowded RAM.

So what typing was done was things like having a naming convention for variables (Integers start with I-N, strings end with "$" and so forth). And over time a lot of people got impatient with even those restrictions.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
 
I've read about this kind of thing at the checkout counter. That's where I met this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic