06-23-2009, 06:45 PM
|
#5 (permalink)
|
|
The Wanderer
Join Date: Jun 2009
Posts: 20
Thanks: 2
|
Quote:
Originally Posted by Salathe
def replaceWith(self, replaceWith):
#store old parent
oldParent = self.parent
#get own index in parents contents array
myIndex = self.parent.contents.index(self)
# if mine and replaceWith's parent is same
if hasattr(replaceWith, 'parent') and replaceWith.parent == self.parent:
# We're replacing this element with one of its siblings.
index = self.parent.contents.index(replaceWith)
if index and index < myIndex:
# Furthermore, it comes before this element. That
# means that when we extract it, the index of this
# element will change.
myIndex = myIndex - 1
#delete me; kill me
self.extract()
#insert replacewith at myIndex
oldParent.insert(myIndex, replaceWith)
|
in this code index method i wanted to use a method which work same as index in php.
here is the php code what i have written.
function replaceWith($replaceWith)
{
$oldParent=$this->parent;
$myIndex=$parent->contents->array_search($this);
if ( $replaceWith->parent==$parent))
{
$index=$this->parent->contents->index($parent,$replaceWith)
if ($index && $index < $myIndex)
{
$myIndex=$myIndex-1;
}
$oldParent->insert($myIndex,$replaceWith) //insert function will be developed letter.
}
}
|
|
|
|