473,394 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Sub-classing unicode: getting the unicode value

Hallöchen!

I sub-classed unicode in an own class called "Excerpt", and now I
try to implement a __unicode__ method. In this method, I want to
get the actual value of the instance, i.e. the unicode string:

def __unicode__(self):
"""Returns the Unicode representation of Excerpt. Note that this is buffered,
so don't be afraid of calling it many times.

:Return:
- Unicode representation of ``self``

:rtype: unicode
"""
if not hasattr(self, "__unicode"):
self.__unicode = super(Excerpt, self).__unicode__()
return self.__unicode

Unfortunately, unicode objects don't have a __unicode__ method.
However, unicode(super(Excerpt, self)) is also forbidden because
super() allows attribute access only (why by the way?).

How does my object get its own value?

Tschö,
Torsten.

--
Torsten Bronger, aquisgrana, europa vetus
Jabber ID: br*****@jabber.org
(See http://ime.webhop.org for further contact info.)
Dec 30 '07 #1
3 1563
On 30 dic, 17:25, Torsten Bronger <bron...@physik.rwth-aachen.de>
wrote:
I sub-classed unicode in an own class called "Excerpt", and now I
try to implement a __unicode__ method. *In this method, I want to
get the actual value of the instance, i.e. the unicode string:
The "actual value of the instance", given that it inherits from
unicode, is... self.
Are you sure you *really* want to inherit from unicode? Don't you want
to store an unicode object as an instance attribute?
Unfortunately, unicode objects don't have a __unicode__ method.
Because it's not needed; unicode objects are already unicode objects,
and since they're immutable, there is no need to duplicate them. If it
existed, would always return self.
However, unicode(super(Excerpt, self)) is also forbidden because
super() allows attribute access only (why by the way?).
(because its purpose is to allow cooperative methods in a multiple
inheritance hierarchy)
How does my object get its own value?
"its own value" is the instance itself

--
Gabriel Genellina
Dec 30 '07 #2
How does my object get its own value?

def __unicode__(self):
return unicode(self)

Regards,
Martin
Dec 30 '07 #3
Hallöchen!

Martin v. Löwis writes:
>How does my object get its own value?

def __unicode__(self):
return unicode(self)
I get an endless recursion with this.

I must admit, though, that I probably overestimate the costs
connected with unicode(my_excerpt) because Gabriel is probably right
that no real conversion takes place. A mere attribute lookup may
still be cheaper, but only very slightly.

Tschö,
Torsten.

--
Torsten Bronger, aquisgrana, europa vetus
Jabber ID: br*****@jabber.org
(See http://ime.webhop.org for further contact info.)
Dec 30 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: tshad | last post by:
I have an example I copied from "programming asp.net" (o'reilly) and can't seem to get the Sub (writefile) to execute. It displays all the response.write lines that are called directly, but not...
3
by: Kathy Burke | last post by:
Hi, I'm tired, so this question may be silly. I have a fairly long sub procedure. Based on one condition, I load another sub with the following: If Session("GRN") = "complete" Then txtScan.Text...
9
by: tshad | last post by:
I have an example I copied from "programming asp.net" (o'reilly) and can't seem to get the Sub (writefile) to execute. It displays all the response.write lines that are called directly, but not...
6
by: Greg Strong | last post by:
Hello All, Is is possible to use an ADO recordset to populate an unbound continuous Subform? I've done some Googling without much luck, so this maybe impossible, but let me try to explain...
6
by: Thom Little | last post by:
Using C# 3.5 I have a form that calls many other sub-forms. Typically there will be five forms open at the same time. If the main form is closed all the sub forms are also closed. Is there...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.