Classes that do operator overloading

View: New views
10 Messages — Rating Filter:   Alert me  

Classes that do operator overloading

by C.T. Matsumoto :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello All,

I'm reading Learning Python's section 'Operator Overloading' and I was
wondering why
class headers that implement and overload are lowercase?

Cheers,

T
_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Re: Classes that do operator overloading

by Alan Gauld :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


"C.T. Matsumoto" <tmatsumoto@...> wrote

> I'm reading Learning Python's section 'Operator Overloading' and I was
> wondering why class headers that implement and overload are lowercase?

I'm not sure what you mean by class headers?
Can you post an example?


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

_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Re: Classes that do operator overloading

by C.T. Matsumoto :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

yes,

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

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.
If the class has a mix, operator overloading and a normal method then
the class name gets starts with a capital.

It's just a detail, but I wanted to know.

T
Alan Gauld wrote:

>
> "C.T. Matsumoto" <tmatsumoto@...> wrote
>> I'm reading Learning Python's section 'Operator Overloading' and I
>> was wondering why class headers that implement and overload are
>> lowercase?
>
> I'm not sure what you mean by class headers?
> Can you post an example?
>
>

_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Re: Classes that do operator overloading

by Hugo Arts :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sat, Nov 7, 2009 at 10:39 AM, C.T. Matsumoto <tmatsumoto@...> wrote:

> yes,
>
> class Foo: # the book says this is a class header
>   pass
>
> 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.
> If the class has a mix, operator overloading and a normal method then
> the class name gets starts with a capital.
>
> It's just a detail, but I wanted to know.
>

class names should always be capitalized, no matter what kind of
methods they have.
The exceptions to this are the built-in types (int, str, list, dict,
etc.). But if you're writing a class yourself,
capitalize it.

quoting PEP 8:

      Almost without exception, class names use the CapWords convention.
      Classes for internal use have a leading underscore in addition.

if you want to know something, anything at all about style
conventions, read PEP 8. It's the definitive python styleguide.

http://www.python.org/dev/peps/pep-0008/

Hugo
_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Re: Classes that do operator overloading

by C.T. Matsumoto :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks Hugo,

Do methods like __add__, __del__, count as built-in types? I'm aware of the
rule you explained and use it and that's why when  I saw:

class indexer():
     def ___getitem__(self, index):
         return index ** 2

I thought I was missing some special style, or rule. The class above is take
from Learning Python, and there are several other examples too.

Thanks,

T
Hugo Arts wrote:

> On Sat, Nov 7, 2009 at 10:39 AM, C.T. Matsumoto <tmatsumoto@...> wrote:
>  
>> yes,
>>
>> class Foo: # the book says this is a class header
>>   pass
>>
>> 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.
>> If the class has a mix, operator overloading and a normal method then
>> the class name gets starts with a capital.
>>
>> It's just a detail, but I wanted to know.
>>
>>    
>
> class names should always be capitalized, no matter what kind of
> methods they have.
> The exceptions to this are the built-in types (int, str, list, dict,
> etc.). But if you're writing a class yourself,
> capitalize it.
>
> quoting PEP 8:
>
>       Almost without exception, class names use the CapWords convention.
>       Classes for internal use have a leading underscore in addition.
>
> if you want to know something, anything at all about style
> conventions, read PEP 8. It's the definitive python styleguide.
>
> http://www.python.org/dev/peps/pep-0008/
>
> Hugo
>
>  

_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Re: Classes that do operator overloading

by Hugo Arts :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sat, Nov 7, 2009 at 12:25 PM, C.T. Matsumoto <tmatsumoto@...> wrote:
> Thanks Hugo,
>
> Do methods like __add__, __del__, count as built-in types? I'm aware of the
> rule you explained and use it and that's why when  I saw:
>

Built-in types are only those classes 'built in' to the python
interpreter. They include int, float, str, list, dict, tuple, and
others. Every class you define yourself (and also most classes in the
standard library) should have a Capitalized class name, no matter what
kind of methods you define for it.

> class indexer():
>    def ___getitem__(self, index):
>        return index ** 2
>
> I thought I was missing some special style, or rule. The class above is take
> from Learning Python, and there are several other examples too.
>

following the PEP 8 guidelines, that class should have been named Indexer.
_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Re: Classes that do operator overloading

by Tim Golden-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hugo Arts wrote:

> On Sat, Nov 7, 2009 at 12:25 PM, C.T. Matsumoto <tmatsumoto@...> wrote:
>> class indexer():
>>    def ___getitem__(self, index):
>>        return index ** 2
>>
>> I thought I was missing some special style, or rule. The class above is take
>> from Learning Python, and there are several other examples too.
>>
>
> following the PEP 8 guidelines, that class should have been named Indexer.


It's worth stressing that word *guideline*: if you're contributing to
the Python core, PEP 8 is de rigueur; another library or project might
have different standards, more or less derived from PEP 8. If you're
writing your own code, you can do whatever you like. The advantage of
following PEP 8 is that there's one less barrier for someone (including
you, later) following your code. But if you fancy lowercase classes,
use lowercase classses. :)

TJG
_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Re: Classes that do operator overloading

by Alan Gauld :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

"C.T. Matsumoto" <tmatsumoto@...> 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@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Re: Classes that do operator overloading

by spir :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Le Sat, 7 Nov 2009 14:06:15 -0000,
"Alan Gauld" <alan.gauld@...> s'exprima ainsi:

> "C.T. Matsumoto" <tmatsumoto@...> 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.

English is terrible. From wiktionary (abstracts)/

head
        The topmost, foremost, or leading part.

header
        The upper portion of a page (or other) layout.
        Text, or other visual information, used to mark off a quantity of text, often titling or summarizing it.
        Text, or other visual information, that goes at the top of a column of information in a table.

headline
        A heading or title of an article.

heading
        The title or topic of a document, article, chapter etc.


Denis
------
la vita e estrany


_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Re: Classes that do operator overloading

by Alan Gauld :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


"spir" <denis.spir@...> wrote

> English is terrible. From wiktionary (abstracts)/

Indeed. and the fact that computer Scientists often abuse English
makes it worse!

> header
> Text, or other visual information, used to mark off a quantity of text,
> often titling or summarizing it.

This is probaly the closest to the usual CS definition.

A class header in C++, Delphi, Objective C and some other
languages is separate to its definition and provides a summary
or statement of the class interface. The implementation of the
class is then done elsewhere (although possibly in the same file).
Thats why I don;t think the class name definition is sufficient to
constitute a "header", it doesn't give any clue about the operations
or attributes - no summary in other words.

A class name and well written docstring could just about be called
a header, but its not common usage in Python and its accuracy
would depend entirely on the quality of the docstring!

All IMHO of course! :-)

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


_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor