ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-tutor
python-tutor
Re: [Tutor] Classes that do operator overloading
by Alan Gauld other posts by this author
Nov 7 2009 6:09AM messages near this date
Re: [Tutor] Classes that do operator overloading | Re: [Tutor] Classes that do operator overloading
"C.T. Matsumoto" <tmatsumoto@[...].net>  wrote 

>  class Foo: # the book says this is a class header
>     pass

Hmm, that's pretty dubious usage of header in my view. 
Its the class definition and there is nothing "header" about it.

>  As for my question it looks like the convention is if a class only has
>  operator overloading then the class receives a lowercase class name.

Hugo and Tim have answered that. Pep 8 says no, but local 
standards may over-rule that.

It is worth pointing out however that classes which only have 
operator overloads in them are pretty rare! Especially ones 
that don't inherit from an existing type or class.

The Indexer example is not very typical, a pseudo list 
where the content that is apparently always twice the index.
I can see it being used to save space where the 
alternative would be a list comprehension like:

myIndexer = [2*n for n in range(bugnum)]

It would also guarantee that you never had an index out of range.

But personally even there I'd generalise it by passing in a function
so that the Indexer could return any function of the index:

class Indexer:
     def __init__(self, func = lambda n: n):
          self.func = func
     def __getitem__(self, index)
          return self.func(index)

doubles = Indexer(lambda n: n*2)
squares = Indexer(lambda n: n*n)
plus3s = Indexer(lambda n: n+3)

etc.

But its certainly not exactly a typical scenario.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  Tutor@[...].org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Thread:
C.T. Matsumoto
Alan Gauld
Alan Gauld
Alan Gauld
Spir
C.T. Matsumoto
Hugo Arts
C.T. Matsumoto
Hugo Arts
Tim Golden

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved