472,103 Members | 1,048 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,103 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 1172
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

59 posts views Thread by Raymond Hettinger | last post: by
2 posts views Thread by Jacek Generowicz | last post: by
99 posts views Thread by David MacQuigg | last post: by
17 posts views Thread by Grant Edwards | last post: by
125 posts views Thread by Raymond Hettinger | last post: by
3 posts views Thread by lovecreatesbeauty | last post: by
6 posts views Thread by John Rivers | last post: by
8 posts views Thread by Frank Rizzo | last post: by
reply views Thread by leo001 | last post: by

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.