[Tutor] argparse: how to use the returned Namespace object?

Walter Prins wprins at gmail.com
Fri Nov 5 02:11:52 CET 2010


On 5 November 2010 00:40, Mac Ryan <quasipedia at gmail.com> wrote:
>
> Thank you Walter. I got it and it works! :)
>

Excellent.  I thought I'd mention here you can also create your own
Namespace object (e.g. if you find accessing __dict__ not ideal or something
you can do your own implementation, which will work as long as it's got the
neccesary features.)


> I had previously already inspected "ns.__dict__" with the "dir()"
> function, but couldn't (and can not) see my parameters neither with
> "dir(ns.__dict__)" nor with "dir(ns.__dict__.items)". This is clearly an
> indication that I misunderstand what the __dict__ is and how it works.
>

You need to distinguish between what __dict__ *is*, and what it *contains*.
dir() does introspection, it inspects what an object in Python *is*, e.g.
displays all the methods and attributes of the object.  It does not however
know anything about what (if anything) the object might contain, in the data
storage sense.

A dictionary object is a specific type of container object, it has many
methods but suffice it to say the data (keys and values) inside it are
obviously not explicitly exposed as attribues of the dict object itself. So
then, dir() on a dict object, shows you the methods and attributes of
dict's, but nothing of what data might be inside the dict.

Similarly when you dir() your own instance of an object with some custom
attributes, then likewise, dir() on the object itself will show you the
members and attribues of that object itself.

Now, Python makes available a read-only, "hidden" attribute on all objects
(e.g. a chiled member/object) called "__dict__", into which it encodes the
actual attributes of that object, as key value pairs.  So... if you dir()
the object *itself*, you get information about *its* attributes, if you
however dir() the dict that's part of that object, you'll get a dict's
properties, not what is contained inside of the dicts.

Best,

Walter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101105/98b75519/attachment.html>


More information about the Tutor mailing list