To 'with' or not to 'with': how is the question ?

Alex Martelli aleaxit at yahoo.com
Sun Sep 3 04:42:21 EDT 2000


"Jason Cunliffe" <jasonic at nomadicsltd.com> wrote in message
news:sr2ance1c5d73 at corp.supernews.com...
    [snip]
> > evilcommondocument = CreateObject("Word.Document")
> > _ = evilcommondocument
> > _.AutoHyphenation = True
> > _.PrintPreview()
> > _.Close()
    [snip]
> hmm.. very interesting. I didn't know you could do that.
> Seems like doing this with _ would be very dangerous.

Then use a different and more readable/explicit short name
for the placeholder.

> Also how do you turn it off/ reset it again?

del _

but a better approach, IMHO, is a for-construct.


> I am not sure if is the abbreviation or the indentation.. maybe it's the
> combination effect I like. Plus the slightly XML-ish quality of
>
> with somenamespace
>     .something
>     .orother
> end with
>
> Obviously this does not work with Python white indentation which I think
is
> brilliant good idea.

so use:

for ns in [somenamespace]:
    ns.something
    ns.orother

You get the abbreviation (to something readable such as ns, or to
the quasi-transparent _ if you prefer) and the indentation.  It's
not normally considered necessary to follow this with a
    del ns
to unbind the name, but you can do it if you want.


> I think the heart of my question is how you direct python to say "use
> this_namespace until further notice"?
> ..Where this_namespace is started by 'with' and further notice is demarked
> by 'endwith'.

Explicit is better than implicit.  Just as in an object's method I
explicitly
say self.foo to refer to its foo field (rather than use just foo, as in many
other OO languages, _implicitly_ using the object's namespace), so I
do in other context -- supply and use an explicit name.


> I am a newbie and still very shaky about objectpaths and namespace in
Python
> perhaps they are the same thing:  name1.name2.name3 ??
>
> This collides with my lack of understanding of
> import
> from somewhere import something
>
> This begs the newbie question: "Hello python expert, what decides your

I'm not a real expert, but...:

> choice of when to use
>  - from somewhere import something

Rare, basically only when I want to make something look like a builtin
function (e.g. because it overrides one).

> - from somewhere import *

Rare, only with those very rare modules explicitly designed and
documented for that usage.

> - import something

The normal way to access a module.

> - import *

I don't think this is valid Python syntax and wouldn't know what
it could mean if it were.


Alex






More information about the Python-list mailing list