Anyone has a nice "view_var" procedure ?

Larry Bates larry.bates at websafe.com
Mon Jan 15 19:42:02 EST 2007


Stef Mientki wrote:
> hello,
> 
> Is there some handy/ nice manner to view the properties of some variable ?
> As a newbie, I often want to see want all the properties of a var,
> and also some corner values (large arrays) etc.
> 
> Probably it's not so difficult,
> but I don't see how to distinguish for example between a string and an
> array. An array has a shape, a string not etc.
> 
> 
> thanks,
> Stef Mientki

Others have given some suggestions, but I thought I'd put in my
thoughts also.  When I first started using Python I was looking for
exactly what you describe.  After using a while, you begin to understand
that the objects in Python can be so complex (lists of dictionaries that
contain lists and other dictionaries, lists of class instances that can
contain other class instances, that can contain...., you get the picture).
You have to begin to think about navigating through this by using dir(),
vars() and pprint.pprint().  Unlike older programming languages there is
no limit to how you can mix/match values within an object.

Also in standard Python lists (arrays?) don't have shape (there are add
on modules that give you this type of feature).  Everything is a
collection of objects.

class foo:
    def __init__(self):
        self.x=1
        self.y=2

l=[[1,2,3], 'test', 6, 3.14159, {'a':1, 'b':2}, foo()]

a list with:
a list as first element
a string as the second element
an integer as the third element
a float as the third element
a dictionary as the fourth element
a class instance as the fifth element

The tutorial is a good place to start if you haven't already looked at
it.

-Larry Bates



More information about the Python-list mailing list