Python "with"

I

Ivan Voras

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.member1 := something;
some.big.structure.or.nested.objects.element.member2 := 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-----
 
J

J. Cliff Dyer

Ivan said:
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.member1 := something;
some.big.structure.or.nested.objects.element.member2 := 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:
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:

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
 
B

Bjoern Schliessmann

Ivan said:
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
 
B

Ben Finney

Ivan Voras said:
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.
 
D

Dustan

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.member1 := something;
some.big.structure.or.nested.objects.element.member2 := 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
 
L

Laurent Pointal

Ivan Voras a écrit :
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.references
filerefs.encoding = "utf-8"
filerefs.name = "MyFileName.txt"
filerefs.use_quotes = True

Isn't it ?
 
I

Ivan Voras

Laurent said:
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.references
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

iD8DBQFG7k3KldnAQVacBcgRAxeaAJ9M5OJxtJk57NNt5BDOo6BcfZpPtwCfW7Rc
zFjHiQvJw2PoBJD6tVO/Bkg=
=HYCZ
-----END PGP SIGNATURE-----
 
J

J. Cliff Dyer

Ivan said:
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
 
P

Peter Otten

Ivan said:
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:
.... for nv in update.iteritems():
.... setattr(obj, *nv)
........ alpha=42,
.... beta="yadda",
.... gamma=None
.... )A(alpha=42, beta='yadda', gamma=None)

Peter
 
L

Laurent Pointal

Ivan Voras a écrit :
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# ?
 
G

Grant Edwards

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

Bruno Desthuilliers

Grant Edwards a écrit :
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 !-)
 
B

Bruno Desthuilliers

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

Colin J. Williams

Ivan said:
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.member1 := something;
some.big.structure.or.nested.objects.element.member2 := 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top