?
Solved

rewrite function in python

Posted on 2012-09-06
4
Medium Priority
?
460 Views
Last Modified: 2012-09-26
    
def temp(self, check_node, node):       
        while getattr(node, 'parent_node', None):
            node = node.parent_node
            if check_node == node:
                return True      

--------
if self.temp(check_node,node):
    do something

Open in new window



How can I rewrite def temp so I can directly use it in the if without having to create a seperate function?
0
Comment
Question by:Dennie
  • 3
4 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 38372761
How deep can your nesting be?

Do you have an example initialized data structure?
0
 
LVL 41

Accepted Solution

by:
HonorGod earned 2000 total points
ID: 38372922
How are your objects stored?
As individual variables / objects?
or in an array / list?

if you have a list, then it might be as simple as:

if node in myObjects :
    doSomething()

For example, if your class is something like:

class thing:
    def __init__( self, name, parent = None ) :
        self.name = name
        if parent :
            self.parent_node = parent
    def setParent( self, parent ) :
        self.parent_node = parent
    def getParent( self ) :
        return self.parent_node
    def temp( self, check_node, node ):       
        while getattr(node, 'parent_node', None):
            node = node.parent_node
            if check_node == node:
                return True
    def hasAncestor( self, node ) :
        here = self
        while getattr( here, 'parent_node', None ) :
            here = here.parent_node
            if here == node :
                return True
        return False

Open in new window


and you use individual variables, then you may have to use the temp / hasAncestor method:

>>> a = thing( 'a' )
>>> b = thing( 'b', a )
>>> c = thing( 'c', b )
>>> d = thing( 'd', c )
>>> e = thing( 'e', d )
>>>
>>> d.hasAncestor( e )
False
>>> d.hasAncestor( a )
True
>>>

Open in new window


On the other hand, if your objects are stored in a list... you could use either the hasAncestor() method, or the "in" operator...

>>> things = [ thing( name ) for name in 'a,b,c,d,e'.split( ',' ) ]
>>> prev = None
>>> for item in things :
...     item.setParent( prev )
...     prev = item
...
>>> things[ -1 ].hasAncestor( things[ 0 ] )
True
>>> things[ -2 ].hasAncestor( things[ -1 ] )
False
>>>
>>> things[ 2 ] in things
True
>>> things[ -1 ] in things[ :-1 ]
False
>>>

Open in new window

0
 
LVL 17

Expert Comment

by:gelonida
ID: 38380795
I don't have an answer, but why do you want to get rid of the function?

It seems to make your code more readable.

At least if you gave the function a proper name like found_node_in_parents
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 38438306
Thanks for the grade & points.

Good luck & have a great day
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Make the most of your online learning experience.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question