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

Jason Cunliffe jasonic at nomadicsltd.com
Sat Sep 2 18:32:12 CEST 2000


> It's unclear whether you value the abbreviation or the indentation here.
Since
> "everything's an object" in Python, the abbreviation is usually very easy
to get just by
> binding a short name to however much of the initial segment of
>
>     name1.name2.name3...
>
> you're interested in.  Use an underscore or two or three for the name, and
it will "look
> indented" too if you squint <wink>:
>
> evilcommondocument = CreateObject("Word.Document")
> _ = evilcommondocument
> _.AutoHyphenation = True
> _.PrintPreview()
> _.Close()
>
> I don't read VBScript, though, so it's possible that the Python version
actually works
> <wink>.

hmm.. very interesting. I didn't know you could do that.
Seems like doing this with _ would be very dangerous.
Also how do you turn it off/ reset it again?

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.

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'.

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
choice of when to use
 - from somewhere import something
- from somewhere import *
- import something
- import *
???"

One of the main beginner problems I find is going across various modules and
ending up with the wrong level of name1.name2.name3...
For example when importing PIL, this was major headache. Many examples in
books seem not to work one day, and then work again the next. Something to
do with import but I am still not sure how..
How can you view a history of the python namespace in a session?
Is this stored somewhere?

I am wondering if there is an elegant clear Python way to approach the
problem, allowing one perhaps to add some smarts at the beginning of a
module which checks how it was loaded and then complies.

This is where a Python 'with' technique could be very handy I think.
By checking the import and then passing the suitable value to the 'with'
variable, one could keep the body of the code as global as possible.

Likely you know a far simpler existing solution to this dilemma, similar to
your example above

thanks
- Jason










More information about the Python-list mailing list