String.digits help!!!

A

Anoop

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
 
S

Simon Forman

Anoop said:
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.)
 
B

bearophileHUGS

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
 
S

Simon Forman

Simon Forman:

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
 
B

bearophileHUGS

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
 
R

Ron Adam

Simon Forman:


It too looks acceptable.



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
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top