No Thoughts about Everything

B

b-blochl

I wonder about the huge traffic in the question how many tuples are
dispensible. I find that a question of only ternary or quarterny order -
use it or use it not (free after Shakespears "to be or not to be). Well,
I had my part on that.

But introducing students to python as a first programming language lists
and tuples are astoninglishy always constant points of confusion as well
as the other points of the citatet list. I will not repeat it now, but
the class-questions are more interesting for me and fundamental for the
choice of python.

A point of vital interest for me are the classes and the handling of
class variables. I have read a lot of discussion threads in the mean
time and the PEP 318 as well. One point of critic will become obsolet
with the possible future change described in PEP 318. (Use of Java-like
modifiers is simpler.)

I find the syntax and handling of classes and class variables (or static
variables) in python not an absolute success. Fist that is a result of
comparing my personal experience with Java in the first place. Second it
is an experience of introductory teaching python. The learning curve is
astoninglishly steep, but it will flatten arriving at classes and
objects in python. Compared with Java I found it just opposit.

Beside, I find the concept of modifiers in Java very attractive. (May be
that concept is not absolutely clean OO - but it is very simple to apply
in real world programming.) Some arguments I found in the discussions
make me arguing that some concepts connected to that modifiers are
absolutely misunderstood. For instance "information hiding" with the use
of the modifier "private" for instance variables and/or methods
basically means that no direct access from the clients is allowed. There
is no doubt, that that concept rescues bugs and improves program
modification. In the discussions very often "information hiding" is only
interpreted as "hidden code". Even compiled Java code can easily be
recovered - start a internet search for appropriate tools. In an
interpreted language like python I see no problem at all. (Guido will
never allow hiding.......)

Let me give you an instance for the use of the Java modifiers static and
final. (There are three "visibility" and seven other modifiers in Java)
That problem is not a constructed and log searched example, but an every
day application in teaching for instance natural or engineering science,
while using Java. I kindly ask for help to transfer that simple, single
line intelligent to python in the best possible way:

static final double c=2.99792458e8;

That is the speed of light in vacuum in scientific (exponential)
notation. I use it in a class as a class variable. That means it will be
shared as a single copy of a variable among all of the objects
instantiated from that class. That is the meaning of the modifier
"static". With final I make it to a constant, that cannot changed by any
code. (double is self speaking, it is not necessary in python - as
anyone knows.) I dont like to use c as global variable. A possible
waywith the following code recipes?

1. modifier final
For instance recipe 5.15 Defining Constants (Alex Martelli) out of the
python cookbook (OReilly, ISBN 0-596-00167-3) shows, how to define
constants. About 10 lines, instead of one modifier - I have not found
another way.
2. modifier static
There is recipe 5.6 i(Alex Martelli, Carel Fellinger) in the cited book
shows how to implement static Methods to make variables static. Three
more lines for a simple modifier.

With some time and by trial and error may be I find the solution. But
there are many Geeks reading python.org. I think for them it is only a
fingersnip to transfer that to the python way? So, here again my Question:
How can I create such a "class constant" (static final / recipe 5.15 and
5.6 in one) in python as short as possible?
(I hope you recognize, why teaching python to beginners becomes tedious
in python arriving at that point?)

Thanks in advance
Bernhard
 
J

John Roth

b-blochl said:
I wonder about the huge traffic in the question how many tuples are
dispensible. I find that a question of only ternary or quarterny order -
use it or use it not (free after Shakespears "to be or not to be). Well,
I had my part on that.

But introducing students to python as a first programming language lists
and tuples are astoninglishy always constant points of confusion as well
as the other points of the citatet list. I will not repeat it now, but
the class-questions are more interesting for me and fundamental for the
choice of python.

Tuples can be difficult if they're not explained properly, and
I don't find a lot of good material around that really brings
out the salient points.

For me at least, the basic thing about tuples is that they're a
quick way of bundling a bunch of objects together when you
don't want to go to the trouble of creating a special purpose
object to hold them.

I think that the use of implicit tuple creation on the return statement
and sequence unpacking make this point quite well.

I find that classifying them as sequences is essentially confusing,
and also stating that they're immutable is, while true, also confusing.
 
K

Keel Thaan

b-blochl said:
static final double c=2.99792458e8;

That is the speed of light in vacuum in scientific (exponential)
notation. I use it in a class as a class variable. That means it will be
shared as a single copy of a variable among all of the objects
instantiated from that class. That is the meaning of the modifier
"static". With final I make it to a constant, that cannot changed by any
code. (double is self speaking, it is not necessary in python - as
anyone knows.) I dont like to use c as global variable.
So, here again my Question:
How can I create such a "class constant" (static final / recipe 5.15 and
5.6 in one) in python as short as possible?


This will create c as an unmodifiable attribute:

c = property(lambda self: 2.99792458e8)


Does that do what you want?
 
J

Josiah Carlson

This will create c as an unmodifiable attribute:
c = property(lambda self: 2.99792458e8)


Does that do what you want?
.... c = property(lambda self: 2.99792458e8)
....
It doesn't quite work.

- Josiah
 
K

Keel Thaan

Josiah said:
... c = property(lambda self: 2.99792458e8)
...

It doesn't quite work.

I suppose I should have mentioned that you have to use a new-style class:
.... c = property(lambda self: 2.99792458e8)
....Traceback (most recent call last):
File "<input>", line 1, in ?
AttributeError: can't set attribute


I don't think the behavior of property() is even officially defined for
old-style classes.
 
J

Josiah Carlson

I don't think the behavior of property() is even officially defined for
old-style classes.

Very good point. I think I'm going to need to play with properties next
weekend.

- Josiah
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top