By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 765 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Python "with"

P: n/a
Hi,

I'm looking for a construct that's similar to (Turbo) Pascal's "with"
statement. I read about the Python's new "with" statement, but I was
dissapointed to learn that it does something different (I still don't
see how it's better than try..except..finally, but that's not my question).

Is there something similar to what I want that's Pythonic enough?

(If you're not familiar with Pascal, here's how it works:

with some.big.structure.or.nested.objects.element do begin
member1 := something;
member2 := something;
end;

which exactly translates to this equivalent:

some.big.structure.or.nested.objects.element.membe r1 := something;
some.big.structure.or.nested.objects.element.membe r2 := something;

i.e. it's a wrist-saver. I think a similar feature exists in C99, for
structs.

I know it can be almost always done by using a temporary variable:

tmp = some.big.structure.or.nested.objects.element
tmp.member1 = something
tmp.member2 = something

but this looks ugly to me.)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFG7YzxldnAQVacBcgRAotkAKDeZ/uHf8WWpE+Lpk3ZM56Pnl892wCgvCOn
FDQ2/IgAA3duJ4R2vz3glpg=
=X0Y+
-----END PGP SIGNATURE-----

Sep 16 '07 #1
Share this Question
Share on Google+
14 Replies


P: n/a
Ivan Voras wrote:
Hi,

I'm looking for a construct that's similar to (Turbo) Pascal's "with"
statement. I read about the Python's new "with" statement, but I was
dissapointed to learn that it does something different (I still don't
see how it's better than try..except..finally, but that's not my question).

Is there something similar to what I want that's Pythonic enough?

(If you're not familiar with Pascal, here's how it works:

with some.big.structure.or.nested.objects.element do begin
member1 := something;
member2 := something;
end;

which exactly translates to this equivalent:

some.big.structure.or.nested.objects.element.membe r1 := something;
some.big.structure.or.nested.objects.element.membe r2 := something;

i.e. it's a wrist-saver. I think a similar feature exists in C99, for
structs.

I know it can be almost always done by using a temporary variable:

tmp = some.big.structure.or.nested.objects.element
tmp.member1 = something
tmp.member2 = something
You can import the members directly, using:
>>from some.big.structure.or.nested.objects.element import member1,
member2

which requires you to know the variable names. But if you want to do it
after the import, your tmp example is the way to go. It would be less
ugly if your variable is named so that it still has some connection to
the purpose of the module.

There are other options, too:
>>import some.big.structure.or.nested.objects.element as tmp
This will still make you prefix tmp to the variables, but you don't need
a separate statement devoted to rebinding the module to a new name.

Personally, I don't find the shorter prefix all that ugly. For me
ugliness and clarity are almost entirely synonymous when it comes to
code, so maintaining the certainty about where the variables came from
is a good thing. I've just started a job where I have to maintain perl
code, so I keep running across things like

use File::Basename;
my $dir = dirname($fullpath);

and I keep wondering where basename came from. Is it a builtin? Was it
from File:: A prefix, no matter how short, how obscure, how
undescriptive, will let you trace exactly where each name came from.
Which I find deeply beautiful. All you have to do is look up the page,
and it's there somewhere. You *can* do the same with Perl, but it's
actually discouraged by the structure of the language. In other words,
the simplest case does the wrong thing.

Python does allow you to import all names from a module, unprefixed but
it leads you into the same ugliness and namespace clobbering that you
get with Perl, so I'm not going to tell you how to do it.

I think the 'import long.name as short' syntax will help your wrists the
most while still retaining the clarity of explicit module referencing.
And you won't have the extraneous assignment (tmp = long.name)
cluttering up your code.

Cheers,
Cliff
Sep 16 '07 #2

P: n/a
Ivan Voras wrote:
I'm looking for a construct that's similar to (Turbo) Pascal's
"with" statement.
Please have a look at the archives -- this is discussed here from
time to time. I think last time it was a Visual BASIC fan that
asked a similar question.
I know it can be almost always done by using a temporary variable:

tmp = some.big.structure.or.nested.objects.element
tmp.member1 = something
tmp.member2 = something

but this looks ugly to me.)
Such big, javaish names are much uglier, IMHO.

Regards,
Björn

--
BOFH excuse #17:

fat electrons in the lines

Sep 16 '07 #3

P: n/a
Ivan Voras <ivoras@__fer.hr__writes:
I know it can be almost always done by using a temporary variable:

tmp = some.big.structure.or.nested.objects.element
tmp.member1 = something
tmp.member2 = something

but this looks ugly to me.)
To me, it looks explicit. Python programmers value explicit code.

In other words, this looks like the right way to do it. The
alternative you're looking for would be to make an implicit reference,
which would make the code harder to follow.

Note that in the specific case of an attribute of a module, the first
lone of the above example can be rewritten as::

from some.big.package.or.nested.module import element as tmp

which is at least as common as the assignment example above.

--
\ "The only tyrant I accept in this world is the still voice |
`\ within." -- Mahatma Gandhi |
_o__) |
Ben Finney
Sep 17 '07 #4

P: n/a
On Sep 16, 3:07 pm, Ivan Voras <ivoras@__fer.hr__wrote:
Hi,

I'm looking for a construct that's similar to (Turbo) Pascal's "with"
statement. I read about the Python's new "with" statement, but I was
dissapointed to learn that it does something different (I still don't
see how it's better than try..except..finally, but that's not my question).

Is there something similar to what I want that's Pythonic enough?

(If you're not familiar with Pascal, here's how it works:

with some.big.structure.or.nested.objects.element do begin
member1 := something;
member2 := something;
end;

which exactly translates to this equivalent:

some.big.structure.or.nested.objects.element.membe r1 := something;
some.big.structure.or.nested.objects.element.membe r2 := something;

i.e. it's a wrist-saver. I think a similar feature exists in C99, for
structs.

I know it can be almost always done by using a temporary variable:

tmp = some.big.structure.or.nested.objects.element
tmp.member1 = something
tmp.member2 = something

but this looks ugly to me.)

signature.asc
1KDownload
>>import this # Note item #5
Sep 17 '07 #5

P: n/a
Ivan Voras a écrit :
<zip>
I know it can be almost always done by using a temporary variable:

tmp = some.big.structure.or.nested.objects.element
tmp.member1 = something
tmp.member2 = something

but this looks ugly to me.)
The ugly part is the 'tmp' name, try to choose a name with a proper
meaning about what it is really, and it become clean and readable:

filerefs = some.big.structure.or.nested.object.with.file.refe rences
filerefs.encoding = "utf-8"
filerefs.name = "MyFileName.txt"
filerefs.use_quotes = True

Isn't it ?

Sep 17 '07 #6

P: n/a
Laurent Pointal wrote:
The ugly part is the 'tmp' name, try to choose a name with a proper
meaning about what it is really, and it become clean and readable:

filerefs = some.big.structure.or.nested.object.with.file.refe rences
filerefs.encoding = "utf-8"
filerefs.name = "MyFileName.txt"
filerefs.use_quotes = True

Isn't it ?
Well, no, but this might be due to personal tastes. At least, I don't
think it's better then some other alternatives. For example, in C99 you
can do:

static struct option_s foo_option = {
.name = "foo",
.type = O_STRING,
.def_value = "default"
};

At least to me, this looks even better than the Pascal's syntax.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFG7k3KldnAQVacBcgRAxeaAJ9M5OJxtJk57NNt5BDOo6 BcfZpPtwCfW7Rc
zFjHiQvJw2PoBJD6tVO/Bkg=
=HYCZ
-----END PGP SIGNATURE-----

Sep 17 '07 #7

P: n/a
Ivan Voras wrote:
Laurent Pointal wrote:
>The ugly part is the 'tmp' name, try to choose a name with a proper
meaning about what it is really, and it become clean and readable:

filerefs = some.big.structure.or.nested.object.with.file.refe rences
filerefs.encoding = "utf-8"
filerefs.name = "MyFileName.txt"
filerefs.use_quotes = True

Isn't it ?

Well, no, but this might be due to personal tastes. At least, I don't
think it's better then some other alternatives. For example, in C99
you can do:

static struct option_s foo_option = {
.name = "foo",
.type = O_STRING,
.def_value = "default"
};

At least to me, this looks even better than the Pascal's syntax.

So basically, what you're saying is you don't like namespace prefixes at
all?

Keeping your namespaces separate will help the clarity of your code
immensely, unless, arguably, you're doing heavy numerical processing,
goes the argument in a recent thread. Probably what will help you the
most is not a fancy trick for getting rid of the namespace, but getting
over your aversion to them. That will make you a better programmer, in
the long run. Debugging will be easier, people will enjoy working with
your code more. Clarity is beautiful. Objectively so. Not just some
lame "in the eye of the beholder" kind of beautiful.

Cheers,
Cliff
Sep 17 '07 #8

P: n/a
Ivan Voras wrote:
Well, no, but this might be due to personal tastes. At least, I don't
think it's better then some other alternatives. For example, in C99 you
can do:

static struct option_s foo_option = {
.name = "foo",
.type = O_STRING,
.def_value = "default"
};

At least to me, this looks even better than the Pascal's syntax.
foo_option = OptionS(
name="foo",
type=O_STRING,
def_value="default"
)

So doesn't the Python analog even look better than C? If so, you don't
need new syntax:
>>def with_(obj, **update):
.... for nv in update.iteritems():
.... setattr(obj, *nv)
....
>>with_(a.long.way.to.tipperary,
.... alpha=42,
.... beta="yadda",
.... gamma=None
.... )
>>a.long.way.to.tipperary
A(alpha=42, beta='yadda', gamma=None)

Peter
Sep 17 '07 #9

P: n/a
Ivan Voras a écrit :
Laurent Pointal wrote:
>The ugly part is the 'tmp' name, try to choose a name with a proper
meaning about what it is really, and it become clean and readable:

filerefs = some.big.structure.or.nested.object.with.file.refe rences
filerefs.encoding = "utf-8"
filerefs.name = "MyFileName.txt"
filerefs.use_quotes = True

Isn't it ?

Well, no, but this might be due to personal tastes. At least, I don't
think it's better then some other alternatives. For example, in C99 you
can do:

static struct option_s foo_option = {
.name = "foo",
.type = O_STRING,
.def_value = "default"
};

At least to me, this looks even better than the Pascal's syntax.
If its at construction time, you can do the same with Python:

class option_s(object) :
def __init__(self,**initializers) :
self.__dict__.update(initializers)

foo_option = option_s(
name = "foo",
type_ = O_STRING,
def_value = "default"
)
And if the class has no such construction idiom, you can play like this:

x = X()
x.__dict__.update(dict(
name = "foo",
type_ = O_STRING,
def_value = "default"
))
Note: this directly manipulate objects attributes - some times its
preffered to use ad-hoc methods.

Note2: I prefer the "namespace.name = value" solution, more readable.

Note3: Its funny to see how Python users tries to change the language,
does this occure with C, C++, Java, C# ?

Sep 17 '07 #10

P: n/a
On 2007-09-17, Laurent Pointal <la*************@limsi.frwrote:
Note3: Its funny to see how Python users tries to change the language,
does this occure with C, C++, Java, C# ?
Yes. I remember somebody I worked with once who write a C
program using a whole pile of macros to make it look like BASIC:

#define IF if (
#define THEN ) {
#define ELSE } else {
#define ENDIF }

and so on...

--
Grant Edwards grante Yow! I am deeply CONCERNED
at and I want something GOOD
visi.com for BREAKFAST!
Sep 17 '07 #11

P: n/a
Grant Edwards a écrit :
On 2007-09-17, Laurent Pointal <la*************@limsi.frwrote:
>Note3: Its funny to see how Python users tries to change the language,
does this occure with C, C++, Java, C# ?

Yes. I remember somebody I worked with once who write a C
program using a whole pile of macros to make it look like BASIC:

#define IF if (
#define THEN ) {
#define ELSE } else {
#define ENDIF }

and so on...
FWIW and IIRC, one of the rationales for C preprocessor macros was to
let Pascal programmers make C looks like Pascal !-)
Sep 17 '07 #12

P: n/a
Laurent Pointal a écrit :
(snip)
Note3: Its funny to see how Python users tries to change the language,
s/Python users/some new Python users/, IMHO.
Sep 17 '07 #13

P: n/a
Ivan Voras wrote:
Hi,

I'm looking for a construct that's similar to (Turbo) Pascal's "with"
statement. I read about the Python's new "with" statement, but I was
dissapointed to learn that it does something different (I still don't
see how it's better than try..except..finally, but that's not my question).

Is there something similar to what I want that's Pythonic enough?

(If you're not familiar with Pascal, here's how it works:

with some.big.structure.or.nested.objects.element do begin
member1 := something;
member2 := something;
end;

which exactly translates to this equivalent:

some.big.structure.or.nested.objects.element.membe r1 := something;
some.big.structure.or.nested.objects.element.membe r2 := something;

i.e. it's a wrist-saver. I think a similar feature exists in C99, for
structs.

I know it can be almost always done by using a temporary variable:

tmp = some.big.structure.or.nested.objects.element
tmp.member1 = something
tmp.member2 = something

but this looks ugly to me.)

The "with" statement in Pascal was a neat feature to use with records.

Unfortunately, in Python 2.5, "with" has a different use.

Colin W.

Sep 17 '07 #14

P: n/a
On Tue, 18 Sep 2007 00:26, Grant Edwards wrote in comp.lang.python
<<13*************@corp.supernews.com>>:
On 2007-09-17, Laurent Pointal <la*************@limsi.frwrote:
>Note3: Its funny to see how Python users tries to change the
language,
>does this occure with C, C++, Java, C# ?

Yes. I remember somebody I worked with once who write a C
program using a whole pile of macros to make it look like BASIC:

#define IF if (
#define THEN ) {
#define ELSE } else {
#define ENDIF }

and so on...
and latin speaking perl programmers use Lingua::Romana::Perligata
<http://www.csse.monash.edu.au/~damian/papers/HTML/Perligata.html>

Totaly insane. Oh, wait, they're perl programmers.

--
Nigel Rowe
A pox upon the spammers that make me write my address like..
rho (snail) fisheggs (stop) name
Sep 17 '07 #15

This discussion thread is closed

Replies have been disabled for this discussion.