Can I add methods to built in types with classes?

Discussion in 'Python' started by CC, Aug 20, 2007.

  1. CC

    CC Guest

    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

    SuSE 9.1 Linux 2.6.5
     
    CC, Aug 20, 2007
    #1
    1. Advertising

  2. 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')): ...
     
    Scott David Daniels, Aug 20, 2007
    #2
    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. Alex Martelli
    Replies:
    3
    Views:
    311
    Achim Dahlhoff
    Nov 29, 2005
  2. Kenneth McDonald
    Replies:
    3
    Views:
    298
    Neil Cerutti
    Oct 25, 2006
  3. lovecreatesbeauty

    Are Built-in Data Types also Classes

    lovecreatesbeauty, Sep 8, 2005, in forum: C++
    Replies:
    3
    Views:
    307
    Mike Wahler
    Sep 9, 2005
  4. Alona
    Replies:
    2
    Views:
    671
  5. Csaba Hoch
    Replies:
    1
    Views:
    294
    andrew cooke
    Jan 31, 2009
Loading...

Share This Page