473,386 Members | 1,819 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,386 software developers and data experts.

Pythonic way to add method alias in subclass


I thought of several ways to add another name for
a method in a subclass ...
#alias.py
class Foo(object):
def nod(self):
print "nodding"

class Bar(Foo):
def __init__(self):
self.agree = self.nod

class Bar2(Foo):
agree = Foo.nod

class Bar3(Foo):
def agree(self):
Foo.nod(self)

def alias(method):
def dec(m):
return method
return dec

class Bar4(Foo):
@alias(Foo.nod)
def agree(self):
pass
b = Bar()
b.agree()

b2 = Bar2()
b2.agree()

b3 = Bar3()
b3.agree()

b4 = Bar4()
b4.agree()
#####################
I am leaning towards Bar2 since it has the least code.

Any thoughts?

Dec 15 '07 #1
2 1769
On Sat, 15 Dec 2007 13:03:33 +0000, Lee Harr wrote:
I thought of several ways to add another name for a method in a subclass
....
class Bar2(Foo):
agree = Foo.nod
....
I am leaning towards Bar2 since it has the least code.
Sure, why not?

Out of curiosity, what's wrong with just calling Bar2.nod()?
--
Steven
Dec 15 '07 #2
Lee Harr a écrit :
I thought of several ways to add another name for
a method in a subclass ...
#alias.py
class Foo(object):
def nod(self):
print "nodding"

class Bar(Foo):
def __init__(self):
self.agree = self.nod
Will create an instance attribute of type method for each instance of
Bar. Which might or not be a problem FWIW.
class Bar2(Foo):
agree = Foo.nod
The One Obvious Way.
class Bar3(Foo):
def agree(self):
Foo.nod(self)
Are you willing to take the penalty of an extra method call ?
def alias(method):
def dec(m):
return method
return dec

class Bar4(Foo):
@alias(Foo.nod)
def agree(self):
pass
Bad case of arbitrary overcomplification IMHO. Nothing with metaclasses
while we're at it ?-)

(snip)
I am leaning towards Bar2 since it has the least code.
Indeed.
Any thoughts?
cf above.
Dec 15 '07 #3

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

Similar topics

4
by: lawrence | last post by:
I'm very unhappy with the error message that I'm giving in this method: /** * 11-23-03 - getter * * We want to run the query against a MySql database and get back a
4
by: Ruud de Jong | last post by:
The question I have is: how safe / future proof / portable is the use of the __subclasses__ method that exists for new-style classes? Background: I thought I had found an easy-to-understand...
10
by: Ramon Felciano | last post by:
Hi -- I'm using the csv module to parse a tab-delimited file and wondered whether there was a more elegant way to skip an possible header line. I'm doing line = 0 reader =...
11
by: Charles Krug | last post by:
I've a function that needs to maintain an ordered sequence between calls. In C or C++, I'd declare the pointer (or collection object) static at the function scope. What's the Pythonic way to...
16
by: 4Space | last post by:
I've hit something of a snag. Problem: In a DSP application we have data profiles with a varying number of points. We have a number of Discrete Fourier transforms that are optimised for a...
12
by: phil hunt | last post by:
Suppose I'm writing an abstract superclass which will have some concrete subclasses. I want to signal in my code that the subclasses will implement certan methods. Is this a Pythonic way of doing...
4
by: Carl J. Van Arsdall | last post by:
It seems the more I come to learn about Python as a langauge and the way its used I've come across several discussions where people discuss how to do things using an OO model and then how to design...
59
by: peter.tornqvist | last post by:
Maybe I'm stupid or maybe I am missing the obvious, but I can't find a way to define a type alias in C# for primitive types (class and interfaces I can inherit from, no problem). I.e I want to...
13
by: Marco Bizzarri | last post by:
Sorry... pressed enter but really didn't want to. As I said, let's say I have a class class A: def __init__(self): self.x = None
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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?
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.