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 607157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:19:49+00:00 2026-05-13T17:19:49+00:00

I want to define a new class that inherit the build in str type,

  • 0

I want to define a new class that inherit the build in str type, and create a method that duplicates the string contents.

How do I get access to the string value assigned to the object of my new class ?

    class str_usr(str):
        def __new__(cls, arg):
            return str.__new__(cls, arg)
        def dub(self):
            # How to modify the string value in self ?
            self.<attr> = self.<attr> + self.<attr>

Thanks for any help 🙂

  • 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-05-13T17:19:49+00:00Added an answer on May 13, 2026 at 5:19 pm

    Strings in Python are immutable, so once you have one string, you can’t change its value. It’s almost the same as if you had a class derived from int, and then you added a method to change the value of the int.

    You can of course return a new value:

    class str_usr(str):
        def dup(self):
            return self + self # or 2 * self
    
    s = str_usr("hi")
    print s # prints hi
    print s.dup() # print hihi
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to define a class that is able to populate itself reading from
I have a base type that I want to inherit from, for all my
Here's the scenario: I want define a base class that allocate some buffer once
What I mean is to define an instance of a type class that applies
I want to create an object that acts as a specific class, such as
I want to create a new element with html5 data attributes defined, however they
I want to define the data.store that store companyList, because I want to reuse
I want to define a generic function for printing contents of std::map like types.
I want to define functions that can be used in the fixedpoint rule. For
I want to define some properties on a class using the [Indexable()] attribute in

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.