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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:30:45+00:00 2026-06-11T17:30:45+00:00

I’m trying to make a simple derived class based on str , with the

  • 0

I’m trying to make a simple derived class based on str, with the addition of an instance variable, flag. For reasons I do not understand, I get an error if I try to pass the flag to the constructor:

>>> class Strvalue(str):
        def __init__(self, content, flag=None):
                str.__init__(self, content)
                self.flag = flag

>>> Strvalue("No problem")
'No problem'

>>> Strvalue("Problem", flag=None)
Traceback (most recent call last):
  File "<pyshell#113>", line 1, in <module>
    Strvalue("Problem", flag=None)
TypeError: str() takes at most 1 argument (2 given)

I’ve checked that in the succesful calls, the Strvalue constructor really does get called– I haven’t mistyped __init__ or something of that sort. So what is going on?

Edit: According to this question (and @Martijn’s answer), the problem is avoided by overriding __new__ as well. The question was why this was happening.

  • 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-11T17:30:46+00:00Added an answer on June 11, 2026 at 5:30 pm

    You need to use __new__ instead of __init__ when subclassing str, see basic customization.

    >>> class Strvalue(str):
    ...     def __new__(cls, content, flag=None):
    ...         inst = str.__new__(cls, content)
    ...         inst.flag = flag
    ...         return inst
    ... 
    >>> Strvalue('foo', True)
    'foo'
    >>> foo = Strvalue('foo', True)
    >>> foo
    'foo'
    >>> foo.flag
    True
    

    Your code doesn’t override str.__new__, so the original str.__new__ constructor is called with your two arguments, and it only accepts one.

    str objects are immutable, they construct a new instance in __new__, which then cannot be changed anymore; by the time __init__ is called, self is an immutable object, so __init__ for str doesn’t make sense. You can still also define an __init__ method, but since you already have __new__, there is really no need to divide the work up across two methods.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I am doing a simple coin flipping experiment for class that involves flipping a
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I'm trying to select an H1 element which is the second-child in its group

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.