473,405 Members | 2,171 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,405 software developers and data experts.

overriding setting

Hello,
this is my first post to the list :-) I've looked around a bit before
asking, and since I haven't found... I'm here to ask my question.

I'm trying to ovveride attribute setting, but I haven't still found
the right way to use all the fancy __get__, __set__ and
__getattribute__ :-)

I would like to retain initialization of an object by use of the = and
not as a function call. A simple example is this:

I want to define an "enhanced" list class, whose items will be other
(built in) lists and providing a "padding" method which fills all the
contained lists to the same lenght.
An example could be:

class myList(list):
def __init__(self):
self._max = None
list.__init__(self)
def pad(self):
for item in self:
if type(item)== list:
while len(item) < self._max:
item.append("")

Now the question is this:
I would like to initialize such an object in this way:
a = myList()
a = [[1, 2, 3], [4, 5, 6, 7]]
a.pad()
# and now a _should_ contain [[1, 2, 3, ""], [4, 5, 6, 7]]
Obviously this doesn't work, because when at the second line I do the
initialization, type(a) becomes <type 'list'>, and so I get the
expected AttributeError since pad cannot be found.

A possible solution could be to create a container class, intercepting
every attribute accession with __getattribute__.
In this case I should refer to Container.myFirstList,
Container.mySecondList, which is ugly because of the need to refer to
Container first...(not to mention that I'm still working on making the
__getattribute__ work properly... :-) )
Do you have any suggestions? or maybe I should simply stop trying to
do that and resort to adding some sort of insert or append method
(this is what I have done for the time being, but I found this
solution less appealing and nice ...)

thanks in advance,
Francesco
--
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as
both victim and villain by the vicissitudes of fate. This visage, no
mere veneer of vanity, is a vestige of the vox populi, now vacant,
vanished. However, this valorous visitation of a bygone vexation
stands vivified, and has vowed to vanquish these venal and virulent
vermin vanguarding vice and vouchsafing the violently vicious and
voracious violation of volition. The only verdict is vengeance; a
vendetta held as a votive, not in vain, for the value and veracity of
such shall one day vindicate the vigilant and the virtuous. Verily,
this vichyssoise of verbiage veers most verbose vis-à-vis an
introduction, so let me simply add that it's my very good honor to
meet you and you may call me V." -- V's introduction to Evey
Jun 6 '07 #1
2 1139
In <ma***************************************@python. org>, Francesco
Guerrieri wrote:
Now the question is this:
I would like to initialize such an object in this way:
a = myList()
a = [[1, 2, 3], [4, 5, 6, 7]]
a.pad()
# and now a _should_ contain [[1, 2, 3, ""], [4, 5, 6, 7]]
Obviously this doesn't work, because when at the second line I do the
initialization, type(a) becomes <type 'list'>, and so I get the
expected AttributeError since pad cannot be found.
You don't initialize in the second line, you just rebind `a` to a
completely different object. Names don't have types in Python, objects do.

`list()` takes an optional argument. Just make sure your derived type
does to and passes this to the base class `__init__()`. Then you can
create an instance like this:

a = MyList([[1, 2, 3], [4, 5, 6, 7]])

Ciao,
Marc 'BlackJack' Rintsch
Jun 6 '07 #2
On 6/6/07, Marc 'BlackJack' Rintsch <bj****@gmx.netwrote:
In <ma***************************************@python. org>, Francesco
Guerrieri wrote:
Now the question is this:
I would like to initialize such an object in this way:
a = myList()
a = [[1, 2, 3], [4, 5, 6, 7]]
a.pad()
# and now a _should_ contain [[1, 2, 3, ""], [4, 5, 6, 7]]
Obviously this doesn't work, because when at the second line I do the
initialization, type(a) becomes <type 'list'>, and so I get the
expected AttributeError since pad cannot be found.

You don't initialize in the second line, you just rebind `a` to a
completely different object. Names don't have types in Python, objects do.

`list()` takes an optional argument. Just make sure your derived type
does to and passes this to the base class `__init__()`. Then you can
create an instance like this:

a = MyList([[1, 2, 3], [4, 5, 6, 7]])
yes it's true that it is not an initialization :-) It's that I hoped
that there was a way to do an init rather than a rebinding of the
name.
Your suggestion is exactly what I have implemented for the time being...
I subclass the builtin list type, I have a pad method which adds the
requested whitespaces at the end, an append method which invokes the
base class append AND calls the pad method, and finally a __call__
which calls the append. Furthermore, I check that the input is
valid... So everything works fine :-)

The only problem is that while coding I have the temptation to write a
= [[...], [...]) rather than a([1, 2, 3], [5,6, 7, 8]). Plus I find
it uglier :-) but if there isn't a reasonable way, I'll give up :-)

thanks,
Francesco
Jun 6 '07 #3

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

Similar topics

3
by: Ali Eghtebas | last post by:
Hi, I have 3 questions regarding the code below: 1) Why can't I trap the KEYDOWN while I can trap KEYUP? 2) Is it correct that I use Return True within the IF-Statement? (I've already read...
1
by: Dietmar Gräbner | last post by:
Hi Right now I'm dealing with derivation by restriction and I have some questions concerning the attribute property use in context of the derivation. Consider following Schema: <xs:schema...
9
by: James Marshall | last post by:
I'm writing a library where I want to override document.write(), but for all document objects; thus, I want to put it in the prototype. I tried Document.prototype.write= my_doc_write ; but it...
2
by: Sergey Ilinsky | last post by:
How can i override all the styles being cascaded applied to some HTML element? For example: I have a stylesheet where DIV styles are described DIV { padding: 10; margin: 10; }
7
by: Greg | last post by:
I have a base class for a label. I want to set a property at the base class level, say AutoSize = true. Then at the form level I want to be able to overide the base class setting so that I can...
4
by: RSH | last post by:
I tried an implementation of overriding a ComboBox control. I am simply trying to avoid it repainting, but I can't seem to get it to work. What am I doing wrong? Please help. Thanks, Ron
1
by: PJ | last post by:
The dropdownlist renders language="javascript" when the autopostback is set to true. This is invalid markup and I would like to remove it by inheriting from the control and overriding the...
1
by: tshad | last post by:
I have a DataGrid where I want the text in the cells to be black. But I want the header to have a black background and white text. The problem is the settings in the DataGrid are not overriding...
3
by: Dan Jacobson | last post by:
Let's say all one can do to override a document's link colors, <a href="FAQ.aspx"><font color="#0000ff">FAQ</font></a> is inject things like: <a href="FAQ.aspx"><span style="color:green"><font...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.