No Thoughts about Everything

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • b-blochl

    No Thoughts about Everything

    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 "informatio n 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 "informatio n 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




  • John Roth

    #2
    Re: No Thoughts about Everything


    "b-blochl" <bblochl2@compu serve.de> wrote in message
    news:mailman.10 6.1077724046.85 94.python-list@python.org ...[color=blue]
    > 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.[/color]

    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.


    [color=blue]
    >
    > Thanks in advance
    > Bernhard
    >
    >
    >
    >[/color]


    Comment

    • Keel Thaan

      #3
      Re: No Thoughts about Everything

      b-blochl wrote:
      [color=blue]
      > 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.[/color]
      [color=blue]
      > 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?[/color]


      This will create c as an unmodifiable attribute:

      c = property(lambda self: 2.99792458e8)


      Does that do what you want?

      Comment

      • Josiah Carlson

        #4
        Re: No Thoughts about Everything

        > This will create c as an unmodifiable attribute:[color=blue]
        >
        > c = property(lambda self: 2.99792458e8)
        >
        >
        > Does that do what you want?[/color]
        [color=blue][color=green][color=darkred]
        >>> class foo:[/color][/color][/color]
        .... c = property(lambda self: 2.99792458e8)
        ....[color=blue][color=green][color=darkred]
        >>> v = foo()
        >>> v.c[/color][/color][/color]
        299792458.0[color=blue][color=green][color=darkred]
        >>> v.c += 1
        >>> v.c[/color][/color][/color]
        299792459.0[color=blue][color=green][color=darkred]
        >>> foo().c[/color][/color][/color]
        299792458.0[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

        It doesn't quite work.

        - Josiah

        Comment

        • Keel Thaan

          #5
          Re: No Thoughts about Everything

          Josiah Carlson wrote:[color=blue][color=green]
          >> This will create c as an unmodifiable attribute:
          >>
          >> c = property(lambda self: 2.99792458e8)
          >>
          >>
          >> Does that do what you want?[/color]
          >
          >[color=green][color=darkred]
          > >>> class foo:[/color][/color]
          > ... c = property(lambda self: 2.99792458e8)
          > ...[color=green][color=darkred]
          > >>> v = foo()
          > >>> v.c[/color][/color]
          > 299792458.0[color=green][color=darkred]
          > >>> v.c += 1
          > >>> v.c[/color][/color]
          > 299792459.0[color=green][color=darkred]
          > >>> foo().c[/color][/color]
          > 299792458.0[color=green][color=darkred]
          > >>>[/color][/color]
          >
          > It doesn't quite work.[/color]

          I suppose I should have mentioned that you have to use a new-style class:
          [color=blue][color=green][color=darkred]
          >>> class foo(object):[/color][/color][/color]
          .... c = property(lambda self: 2.99792458e8)
          ....[color=blue][color=green][color=darkred]
          >>> v = foo()
          >>> v.c[/color][/color][/color]
          299792458.0[color=blue][color=green][color=darkred]
          >>> v.c += 1[/color][/color][/color]
          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.

          Comment

          • Josiah Carlson

            #6
            Re: No Thoughts about Everything

            > I don't think the behavior of property() is even officially defined for[color=blue]
            > old-style classes.[/color]

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

            - Josiah

            Comment

            Working...