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

  • Home
  • SEARCH
  • 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 6964183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:53:32+00:00 2026-05-27T15:53:32+00:00

I have the following class in python class myTest: def __init__(self, str): self.str =

  • 0

I have the following class in python

class myTest:
    def __init__(self, str):
        self.str = str

    def __unicode__(self):
        return self.str

and in some other file a instantiate myTest to try out the unicode() method

import myClass


c = myClass.myTest("hello world")

print c

as print out I get <myClass.myTest instance at 0x0235C8A0> however if I overrider __str__() I will get hello world as output. My question is, how should I write the overrider for __unicode__() if I want it to output string instead?

  • 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-27T15:53:33+00:00Added an answer on May 27, 2026 at 3:53 pm

    Generally it is done like this:

    class myTest:
        def __init__(self, str):
            self.str = str
    
        def __unicode__(self):
            return self.str
        def __str__(self):        
            return unicode(self).encode('utf-8')
    

    This is because __unicode__ is not called implicitly in they way that __str__ and __repr__ are. It is called under the hood by the built-in function unicode, so if you don’t define __str__ you would have to do:

    print unicode(c)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Python, consider I have the following code: class SuperClass(object): def __init__(self, x): self.x
I have the following base class: class NeuralNetworkBase: def __init__(self, numberOfInputs, numberOfHiddenNeurons, numberOfOutputs): self.inputLayer
I have the following chunk of python code: import hashlib class User: def _set_password(self,
I have the following situation in my python code: class Parent(object): def run(self): print
Suppose we have the following class hierarchy: class ClassA: @property def foo(self): return hello
I have the following IronPython code. class Hello: def __init__(self): pass def add(self, x,
I have the following model: class UptimeManager(models.Manager): def with_length(self): Get querySet of uptimes sorted
Consider the following class: class SquareErrorDistance(object): def __init__(self, dataSample): variance = var(list(dataSample)) if variance
Lets assume we have a class in python: class A(object): def __del__(self): print Del!
Consider the following class : class Token: def __init__(self): self.d_dict = {} def __setattr__(self,

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.