Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8590229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:14:52+00:00 2026-06-11T23:14:52+00:00

I would like to be able to add attributes to characters within a str

  • 0

I would like to be able to add attributes to characters within a str or unicode string, chop the string and move pieces around, and have the attributes still be present at the end.

Would this be possible (and best) with a subclass of str, unicode or possibly basestring?

Example:

s = u"hello world"
s[6].foo = u'bar'
s2 = s.split(' ')
assert(s2 == u'world')
assert(s2[0].foo == u'bar')

Thanks for thoughts!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:14:54+00:00Added an answer on June 11, 2026 at 11:14 pm

    You can try using that as a starter:

    class Mine(unicode):
        #
        def __init__(self, *args, **kwargs):
            super(Mine, self).__init__(*args, **kwargs)
        #
        def __setattr__(self, attr, value):
            try:
                super(Mine, self).__setattr__(attr, value)
            except AttributeError:
                self.__dict__[attr] = value
        #
        def __getattr__(self, attr):
            try:
                super(Mine, self).__getattr__(attr)
            except AttributeError:
                try:
                    return self.__dict__[attr]
                except KeyError:
                    raise AttributeError
    
        def __getitem__(self, item):
            obj = Mine(super(Mine, self).__getitem__(item))
            obj.__dict__ = self.__dict__
            return obj
    

    Of course, you’ll have to create a specific split method, where each item of the output list will be a Mine object:

    def split(self, arg=' '):
        result = []
        for item in super(Mine, self).split(arg):
            i = Mine(item)
            i.__dict__ = self.__dict__
            result.append(i)
        return result
    

    The generic idea is to overload every single method of the parent class (at least, the ones you’re really interested in) so that it returns an instance of your class and inherits the __dict__ of the caller… That can be a lot of work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to be able to add the help button onto my winform,
I would like to be able to add a new SQL LOGIN and name
I would like to be able to add a click event to a entity
I would like to be able to add to a custom class in the
I would like to add a uitoolbar able to slide -in from the top
I would ideally like to be able to add (very repetitive) C/C++ code to
What I would like to do, is be able to pass two attributes to
In my company form, I would like to be able to add products directly.
I have an abstract class which I would like to be able to expose
I have a class that contains some private attributes. What I would like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.