String.digits help!!!

Discussion in 'Python' started by Anoop, Aug 8, 2006.

  1. Anoop

    Anoop Guest

    Hi All

    Hope u all might have come across the string deprecation thought of in
    Python 3.0.

    For example : string.lower(str) needs to be some thing like
    str.lower().

    Can some one help me out whether such a change in the common python
    would require
    "string.digits" to be changed. If yes wat would be ur suggestions

    Thanks for ur inputs

    Anoop
     
    Anoop, Aug 8, 2006
    #1
    1. Advertising

  2. Anoop

    Simon Forman Guest

    Anoop wrote:
    > Hi All
    >
    > Hope u all might have come across the string deprecation thought of in
    > Python 3.0.
    >
    > For example : string.lower(str) needs to be some thing like
    > str.lower().
    >
    > Can some one help me out whether such a change in the common python
    > would require
    > "string.digits" to be changed. If yes wat would be ur suggestions
    >
    > Thanks for ur inputs
    >
    > Anoop


    Some of the functions in module string are being deprecated in favor of
    new methods on the str type itself. The digits variable of the string
    module is just a string of the '0123456789' digits. It's unlikely to
    be deprecated since it doesn't make much sense to make it an attribute
    of the str type.

    Also, to answer another question of yours, observe:

    |>> import string
    |>> for ch in string.digits:
    .... print ch
    ....
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9


    the form "for var in string:" simply assigns each character (string of
    length one, technically) in the string to the var variable at each
    iteration.

    HTH,
    ~Simon

    (BTW, "ur" is "your" and "u" is "you". I'm sorry to nitpick, but it's
    a personal idiosyncrasy of mine to be bothered by such.)
     
    Simon Forman, Aug 8, 2006
    #2
    1. Advertising

  3. Anoop

    Guest

    Simon Forman:
    > It's unlikely to
    > be deprecated since it doesn't make much sense to make it an attribute
    > of the str type.


    Why?

    Thank you,
    bearophile
     
    , Aug 8, 2006
    #3
  4. Anoop

    Simon Forman Guest

    wrote:
    > Simon Forman:
    > > It's unlikely to
    > > be deprecated since it doesn't make much sense to make it an attribute
    > > of the str type.

    >
    > Why?
    >
    > Thank you,
    > bearophile


    Let me toss the question back at you: Does it make sense to you that
    str should have this attribute? Why?


    I'm not advocating for or against one thing or another, and I'm
    certainly not in a position to influence the future development of
    Python (anymore than any of us, that is :) ) but FWIW, here's my
    $0.02:

    My "feeling" is that since str is a Type, and not a data store, it
    doesn't make much sense to me to tack on a bunch of read-only data
    attributes that aren't involved in it's role as a string data type just
    to have a slightly more convenient way of accessing them.

    There are twelve "constants" defined in the string module. If they
    were all attributes of the str type it would, for me, conceptually
    "clutter up" my mental model of str, string, and python itself.

    I like "from string import digits" well enough, and, as a test of my
    mental model, I just used IDLE's "open module" command to find and open
    string.py and discovered:

    # Some strings for ctype-style character classification
    whitespace = ' \t\n\r\v\f'
    lowercase = 'abcdefghijklmnopqrstuvwxyz'
    uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    letters = lowercase + uppercase
    ascii_lowercase = lowercase
    ascii_uppercase = uppercase
    ascii_letters = ascii_lowercase + ascii_uppercase
    digits = '0123456789'
    hexdigits = digits + 'abcdef' + 'ABCDEF'
    octdigits = '01234567'
    punctuation = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
    printable = digits + letters + punctuation + whitespace

    Exactly what I expected to find!

    Really, I wouldn't be too badly shocked to discover the above string
    "constants" in dir(str), so it's kind of "six of one, a half dozen of
    the other". But on the same token, "if it's not broken, don't fix it."

    Further, to make these "constants" into attributes of str, someone
    would have to modify the str type at the C level to put these strings
    "on" it. Since they are str's themselves, it seems to me (having done
    only trivial work with the C API of python) that that someone would
    have to A) create a "first" str type, B) create these 12 strings using
    that first type, C) attach them to the str type to create a "final" str
    type. And all this would have to be done at startup, I think, even if
    my code didn't use these attributes. It seems messy to me.

    So to sum up, IMO, a module (rather than a type) is a good place for
    these things, a module with some variable assignments is completely
    within my mental model of python (i.e. no special case (of strings
    being attributes of their own type (!?))), and no one has to do
    anything special to make the current set up work.



    By the way, I read your post a few days ago concerning adding similar
    data attributes to ints and floats and found it very interesting but
    not compelling.

    While I would love to have the kind information you mentioned
    available, I've never needed it in practice (I've only ever used
    sys.maxint maybe a dozen times) and I wouldn't mind accessing it from a
    module (perhaps math.. math.infinity, math.epsilon, etc., just like
    math.pi and math.e.)

    I look forward to hearing your thoughts an the subject. I hope mine
    haven't been too silly.. :)

    Regards,
    ~Simon
     
    Simon Forman, Aug 8, 2006
    #4
  5. Anoop

    Guest

    Simon Forman:

    > accessing it from a
    > module (perhaps math.. math.infinity, math.epsilon, etc., just like
    > math.pi and math.e.)


    It too looks acceptable.


    > I look forward to hearing your thoughts an the subject.


    Thank you, but I am not expert enough on such topics to give you good
    comments, so I keep the muzzle shut.

    Bye,
    bearophile
     
    , Aug 8, 2006
    #5
  6. Anoop

    Ron Adam Guest

    wrote:
    > Simon Forman:
    >
    >> accessing it from a
    >> module (perhaps math.. math.infinity, math.epsilon, etc., just like
    >> math.pi and math.e.)

    >
    > It too looks acceptable.
    >
    >
    >> I look forward to hearing your thoughts an the subject.

    >
    > Thank you, but I am not expert enough on such topics to give you good
    > comments, so I keep the muzzle shut.
    >
    > Bye,
    > bearophile
    >


    I've been thinking along this lines as well, but with the more general
    question of when should a method or attribute be included in a type or
    class and when shouldn't it. I think that's an important question and
    having some clear guidelines for that will help me to write better
    programs in general.

    A few general rules I've found to be mostly true is..

    (1) If a class method does not have a reference to 'self' or an
    attribute or other method of self. It probably should be a
    function instead.

    This is more common than you might think because class's are also used
    as containers to hold a collection of functions and/or values. I think
    its useful to keep these collections separate from "objects". It keeps
    the objects simpler, and the collection of functions and values clearer.
    The difficulty is when you mix the two together, that's when you begin
    to get a cluttered class that is difficult to understand.

    (2) If no method of a class has a reference to an attribute,
    then the attribute should probably not be part of the class
    or type.


    On the flip side, the inverse of these rules does not necessarily mean
    something should be in the class. But maybe...

    (3) If an value is not useful by other class's or functions.
    It probably should be an attribute of the class.

    These are just guide lines of course, but by keeping them in mind, I do
    think it has helped me to abstain from writing cluttered class's.

    Maybe there are other guidelines like these that are helpful?

    Cheers,
    Ron
     
    Ron Adam, Aug 9, 2006
    #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. Anoop
    Replies:
    4
    Views:
    395
    Anoop
    Jul 26, 2006
  2. zion_zii

    Obtaining digits from bit string

    zion_zii, Apr 15, 2006, in forum: C Programming
    Replies:
    5
    Views:
    337
    Thad Smith
    Apr 16, 2006
  3. loris_p
    Replies:
    3
    Views:
    364
    Roedy Green
    Jun 15, 2007
  4. Tomás Ó hÉilidhe

    String of digits, certain radix, perform division

    Tomás Ó hÉilidhe, Oct 25, 2008, in forum: C Programming
    Replies:
    5
    Views:
    410
    Thad Smith
    Oct 28, 2008
  5. Roy Smith

    Random string of digits?

    Roy Smith, Dec 25, 2011, in forum: Python
    Replies:
    18
    Views:
    599
    Chris Angelico
    Dec 26, 2011
Loading...

Share This Page