Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > String.digits help!!!

Reply
Thread Tools

String.digits help!!!

 
 
Anoop
Guest
Posts: n/a
 
      08-08-2006
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

 
Reply With Quote
 
 
 
 
Simon Forman
Guest
Posts: n/a
 
      08-08-2006
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.)

 
Reply With Quote
 
 
 
 
bearophileHUGS@lycos.com
Guest
Posts: n/a
 
      08-08-2006
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

 
Reply With Quote
 
Simon Forman
Guest
Posts: n/a
 
      08-08-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
bearophileHUGS@lycos.com
Guest
Posts: n/a
 
      08-08-2006
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

 
Reply With Quote
 
Ron Adam
Guest
Posts: n/a
 
      08-09-2006
(E-Mail Removed) 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









 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments