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

Decorator Syntax For Recursive Properties

Consider the following class:

class Node(object):
def __init__(self, name, parent=None):
self.name = name
self.parent = parent

def _ancestors(self, ants=None):
if ants is None:
ants = []
else:
ants.insert(0, self.name)
if self.parent is None:
return ants
return self.parent._ancestors(ants)
ancestors = property(_ancestors)

The ancestor property generates a list ancestor nodes by having each parent
examine its own name recursively. The recursion is managed by calling the
method underlying the parent property, rather than calling the property
directly.

Is it possible to rewrite this property using decorator syntax? Does the
@property decorator create some underlying method that I can call directly?

Alternately, perhaps there is a way to rewrite the recursion so that such a
call is unnecessary? Note that the property should not add its own name if
it is the originating node (the node trying to find _its_ ancestors). So
something like this didn't work for me:

@property
def ancestors(self):
if self.parent is None:
return [self.name]
return [self.name] + self.parent.ancestors

In other words, since there is no longer a list passing from child to parent
during the recursion, is there a graceful way to examine the state of the
ancestor list so far?
Thanks,
Jeffrey
Jul 19 '05 #1
2 1663
Jeffrey Froman wrote:
it is the originating node (the node trying to find its ancestors). So
something like this didn't work for me:

@property
def*ancestors(self):
if*self.parent*is*None:
return*[self.name]
return*[self.name]*+*self.parent.ancestors


But this will, I suppose:

@property
def ancestors(self):
if self.parent:
return self.parent.ancestors + [self.parent]
return []

A non-recursive variant:

@property
def ancestors(self):
result = []
node = self.parent
while node:
result.append(node)
node = node.parent
result.reverse()
return result

Peter

Jul 19 '05 #2
Peter Otten wrote:
something like this didn't work for me:
<snip> But this will, I suppose:

@property
def ancestors(self):
if self.parent:
return self.parent.ancestors + [self.parent]
return []

A non-recursive variant:

@property
def ancestors(self):
result = []
node = self.parent
while node:
result.append(node)
node = node.parent
result.reverse()
return result


Indeed, both of these patterns suit my needs. Thanks very much for the
eye-opening insights.

Jeffrey
Jul 19 '05 #3

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

Similar topics

14
by: Sandy Norton | last post by:
If we are going to be stuck with @decorators for 2.4, then how about using blocks and indentation to elminate repetition and increase readability: Example 1 --------- class Klass: def...
24
by: Steven Bethard | last post by:
I think one of the biggest reasons we're having such problems coming to any agreement on decorator syntax is that each proposal makes a number of syntax decisions, not just one. For decorators, I...
11
by: Ville Vainio | last post by:
It might just be that @decorator might not be all that bad. When you look at code that uses it it's not that ugly after all. A lot of the furor about this is probably because it happened so...
37
by: Bengt Richter | last post by:
ISTM that @limited_expression_producing_function @another def func(): pass is syntactic sugar for creating a hidden list of functions. (Using '|' in place of '@' doesn't change the picture...
7
by: Steven Bethard | last post by:
So here's the state of the decorator debate as I see it: *** Location GvR pretty strongly wants decorators before the function: ...
41
by: John Marshall | last post by:
How about the following, which I am almost positive has not been suggested: ----- class Klass: def __init__(self, name): self.name = name deco meth0: staticmethod def meth0(x):
22
by: Ron_Adam | last post by:
Hi, Thanks again for all the helping me understand the details of decorators. I put together a class to create decorators that could make them a lot easier to use. It still has a few glitches...
19
by: Kay Schluehr | last post by:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
0
by: Miguel Perez | last post by:
Please critique this tail call optimizing decorator I've written. I've tried to fix the pitfalls of other proposed decorators, and the result is this one that supports mutual recursion, does not...
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
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
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
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...
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.