473,385 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Can I add methods to built in types with classes?

CC
Hi:

I've gotten through most of the "9. Classes" section of the tutorial. I
can deal with the syntax. I understand the gist of what it does enough
that I can play with it. But am still a long way from seeing how I can
use this OOP stuff.

But I have one idea. Not that the functional approach isn't workable,
but I have a situation where I need to test if all the characters in a
string are in the set of hexadecimal digits.

So I wrote:
------------------------------------------
from string import hexdigits

def ishex(word):
for d in word:
if d not in hexdigits: return(False)
else return(True)
------------------------------------------

Then I can do this to check if a string is safe to pass to the int()
function without raising an exception:

if ishex(string):
value = int(string, 16)

But can I create a class which inherits the attributes of the string
class, then add a method to it called ishex()? Then I can do:

if string.ishex():
value = int(string, 16)

The thing is, it doesn't appear that I can get my hands on the base
class definition/name for the string type to be able to do:

---------------------------------------
class EnhancedString(BaseStringType):
def ishex(self):
for d in word:
if d not in hexdigits: return(False)
else return(True)
---------------------------------------

Thanks.

--
_____________________
Christopher R. Carlen
cr***@bogus-remove-me.sbcglobal.net
SuSE 9.1 Linux 2.6.5
Aug 19 '07 #1
1 1206
CC wrote:
... But am still a long way from seeing how I can use this OOP stuff.
... I wrote:
from string import hexdigits
def ishex(word):
for d in word:
if d not in hexdigits: return(False)
else return(True)
Then I can do this to check if a string is safe to pass to the int()
function without raising an exception:
if ishex(string):
value = int(string, 16)
The Pythonic way to do this is simply:
try:
value = int(string, 16)
except ValueError:
<something else>
... Can I create a class which inherits the attributes of the string
class, then add a method to it called ishex()? ...
The thing is, it doesn't appear that I can get my hands on the base
class definition/name for the string type to be able to ....
class MyStr(str):
def ishex(self):
try:
value = int(self, 16)
except ValueError:
return False
return True
# in fact, you could even say
# class MyStr(type('123')): ...

Aug 19 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

59
by: Raymond Hettinger | last post by:
Please comment on the new PEP for reverse iteration methods. Basically, the idea looks like this: for i in xrange(10).iter_backwards(): # 9,8,7,6,5,4,3,2,1,0 <do something with i> The...
2
by: Jacek Generowicz | last post by:
Functions defined in Python have type types.FunctionType, and are descriptors whose __get__ method turns them into bound or unbound methods. Functions defined in extension modules have type...
99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
17
by: Grant Edwards | last post by:
I'm trying to figure out how to sort a list, and I've run into a problem that that I have tripped over constantly for years: where are the methods of basic types documented? The only thing I can...
125
by: Raymond Hettinger | last post by:
I would like to get everyone's thoughts on two new dictionary methods: def count(self, value, qty=1): try: self += qty except KeyError: self = qty def appendlist(self, key, *values): try:
3
by: lovecreatesbeauty | last post by:
Prof. Bjarne Stroustrup said "built-in data types have default constructor" at §10.4.2 in `TC++PL, special ed.'. He also said that "built-in data types are not classes" at §11.4 in `TC++PL,...
2
by: Mark | last post by:
I have it in my head that I saw a marketing document from Microsoft with the total number of classes and methods in the .NET Framework. Something like 70,000 methods? I don't need precise numbers,...
6
by: John Rivers | last post by:
hi, here is how to do it and restore sanity to aspx html rendering: (please only reply with sensible architectural discussion - juan) put this at the end of an aspx file (or use an include at...
8
by: Frank Rizzo | last post by:
Is there a setting in VS2005 to quickly locate methods that are unused (maybe through compiler warnings)? If not, any utilities out there that do that? Thanks
55
by: tonytech08 | last post by:
How valuable is it that class objects behave like built-in types? I appears that the whole "constructor doesn't return a value because they are called by the compiler" thing is to enable...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.