No Thoughts about Everything

Discussion in 'Python' started by b-blochl, Feb 25, 2004.

  1. b-blochl

    b-blochl Guest

    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
     
    b-blochl, Feb 25, 2004
    #1
    1. Advertising

  2. b-blochl

    John Roth Guest

    "b-blochl" <> wrote in message
    news:...
    > 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.



    >
    > Thanks in advance
    > Bernhard
    >
    >
    >
    >
     
    John Roth, Feb 25, 2004
    #2
    1. Advertising

  3. b-blochl

    Keel Thaan Guest

    b-blochl wrote:

    > 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?
     
    Keel Thaan, Feb 25, 2004
    #3
  4. > This will create c as an unmodifiable attribute:
    >
    > c = property(lambda self: 2.99792458e8)
    >
    >
    > Does that do what you want?


    >>> class foo:

    .... c = property(lambda self: 2.99792458e8)
    ....
    >>> v = foo()
    >>> v.c

    299792458.0
    >>> v.c += 1
    >>> v.c

    299792459.0
    >>> foo().c

    299792458.0
    >>>


    It doesn't quite work.

    - Josiah
     
    Josiah Carlson, Feb 25, 2004
    #4
  5. b-blochl

    Keel Thaan Guest

    Josiah Carlson wrote:
    >> This will create c as an unmodifiable attribute:
    >>
    >> c = property(lambda self: 2.99792458e8)
    >>
    >>
    >> Does that do what you want?

    >
    >
    > >>> class foo:

    > ... c = property(lambda self: 2.99792458e8)
    > ...
    > >>> v = foo()
    > >>> v.c

    > 299792458.0
    > >>> v.c += 1
    > >>> v.c

    > 299792459.0
    > >>> foo().c

    > 299792458.0
    > >>>

    >
    > It doesn't quite work.


    I suppose I should have mentioned that you have to use a new-style class:

    >>> class foo(object):

    .... c = property(lambda self: 2.99792458e8)
    ....
    >>> v = foo()
    >>> v.c

    299792458.0
    >>> v.c += 1

    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.
     
    Keel Thaan, Feb 25, 2004
    #5
  6. > 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
     
    Josiah Carlson, Feb 29, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. zetasum
    Replies:
    0
    Views:
    482
    zetasum
    Feb 18, 2005
  2. zetasum
    Replies:
    0
    Views:
    459
    zetasum
    Feb 18, 2005
  3. New_aspect

    Aspect oriented Everything?

    New_aspect, Aug 22, 2003, in forum: Perl
    Replies:
    5
    Views:
    1,099
    Robert Will
    Aug 31, 2003
  4. Dave Brueck

    Re: No Thoughts about Everything

    Dave Brueck, Feb 25, 2004, in forum: Python
    Replies:
    3
    Views:
    291
    Dang Griffith
    Feb 26, 2004
  5. bblochl

    No Thoughts about Everything

    bblochl, Feb 26, 2004, in forum: Python
    Replies:
    1
    Views:
    296
    Paul Rubin
    Feb 26, 2004
Loading...

Share This Page