My python annoyances so far

F

flifus

Hi all. I'm learning python these days. I'm going to use this thread
to post, from time to time, my annoyances with python. I hope someone
will clarify things to me where I have misunderstood them.

Annoyances:

1. Underscores! What's the deal with that? Especially those double
underscores. The best answer I read on this is that the double
underscores denotes special methods that the interpreter may
automatically use. For example, 4+4 get expanded by the interpreter to
4.__add__(4).

2. There are modules, there are functions, and there are classes-
methods! Wouldn't it have been easier had everything either been a
function or a class method?
 
D

Diez B. Roggisch

Hi all. I'm learning python these days. I'm going to use this thread
to post, from time to time, my annoyances with python. I hope someone
will clarify things to me where I have misunderstood them.

Annoyances:

1. Underscores! What's the deal with that? Especially those double
underscores. The best answer I read on this is that the double
underscores denotes special methods that the interpreter may
automatically use. For example, 4+4 get expanded by the interpreter to
4.__add__(4).

http://docs.python.org/ref/specialnames.html

2. There are modules, there are functions, and there are classes-
methods! Wouldn't it have been easier had everything either been a
function or a class method?

I'm not sure what you mean here. modules are modules, they can contain
functions, classes, top-level code. Functions and class-methods are
similar, but there are some differences that make sense. But I'm not
sure if you really _mean_ class methods, given that you confuse so much.

The python community is a unusually friendly bunch of people. Yet you
should consider not calling things you don't understand "annoyances".

Diez
 
L

Larry Bates

Hi all. I'm learning python these days. I'm going to use this thread
to post, from time to time, my annoyances with python. I hope someone
will clarify things to me where I have misunderstood them.

Annoyances:

1. Underscores! What's the deal with that? Especially those double
underscores. The best answer I read on this is that the double
underscores denotes special methods that the interpreter may
automatically use. For example, 4+4 get expanded by the interpreter to
4.__add__(4).

2. There are modules, there are functions, and there are classes-
methods! Wouldn't it have been easier had everything either been a
function or a class method?

1. Underscores. __add__ is a special method that gets called when you
want to add two objects together. __init__ is the initalizer method
that gets called when a class is instantiated. Special methods begin
and end with double underlines. __variable is a class (or instance)
variable that shouldn't be manipulated outside the class that is
somewhat equivalent to private variables in other languages. I say
somewhat because there is always a way to get to any variable, even
the double underline ones. _variable is a class (or instance) variable
that is internal to the class an normally shouldn't depended upon
or manipulated from the outside unless the caller really knows what
they are doing.

2. Modules are collections of code. They can be functions or classes.
Functions are functions, classes are classes, you need both in an
object oriented language. Functions alone will not suffice. If you
want to ignore classes at the beginning, please do so.

Suggestion: if you are just starting please accept that there is a
reason for Python being different from almost all "traditional" compiled
languages. If you take some time you will come to understand and,
if you are like most, love the differences. I've been creating a
cross-language COM object and after writing unit tests in VB, Delphi, and
Python I am once again reminded why I love Python. I have more lines
of declarations in the Delphi version than in the Python program for some
tests.

-Larry
 
J

James Stroud

Hi all. I'm learning python these days. I'm going to use this thread
to post, from time to time, my annoyances with python.

Please start a new thread for each annoyance. Overuse of a single thread
is an annoyance to a great many people.
I hope someone
will clarify things to me where I have misunderstood them.

Perhaps substitute "annoyance" with "misunderstanding" to garner the
friendliness of your python peers.
Annoyances:

You mean "Misunderstandings:"
1. Underscores! What's the deal with that? Especially those double
underscores. The best answer I read on this is that the double
underscores denotes special methods that the interpreter may
automatically use. For example, 4+4 get expanded by the interpreter to
4.__add__(4).

The double underscores are probably not the most aesthetic feature of
the language. By the time you know how and when to use them, they will
not bother you at all and so you will be scratching #1 off your list.
2. There are modules, there are functions, and there are classes-
methods! Wouldn't it have been easier had everything either been a
function or a class method?

Do you mean "unbound methods" or "class methods"? They are essentially
the same as functions unless you get into the internals of python, which
means you are going out of your way to look for annoyances. Such
behavior will lead to frustration and, eventually, abject ennui.

So you should strike your latter point off your list of
annoyances...misunderstandings. For example:


py> class Thing(object):
.... doit = classmethod(doit)
....
py> Thing.doit(88)
Param is "param".
py> Thing.doit(object())
Param is "param".
py> def doit(athing, param):
.... print 'Param is "%s".' % param
....
py> class Thing(object):
.... doit = doit
....
py> t = Thing()
py> t.doit(4)
Param is "4".



If your latter misunderstanding is about the lack of actual class
methods, then consider:


py> class Thing(object):
.... doit = classmethod(doit)
....
py> Thing.doit(4)
Param is "4".


James
 
S

Steven D'Aprano

Hi all. I'm learning python these days. I'm going to use this thread
to post, from time to time, my annoyances with python. I hope someone
will clarify things to me where I have misunderstood them.

Annoyances:

1. Underscores! What's the deal with that? Especially those double
underscores. The best answer I read on this is that the double
underscores denotes special methods that the interpreter may
automatically use. For example, 4+4 get expanded by the interpreter to
4.__add__(4).


I know! It's such a trial, especially since my keyboard is broken and when
I type an underscore I get a small electric shock.

2. There are modules, there are functions, and there are classes-
methods! Wouldn't it have been easier had everything either been a
function or a class method?

What really annoys me is the way function names use vowels _and_ *ow!*
consonants. Some of them even use the letter Y, which sometimes is a
vowel and sometimes is a consonant. That just makes me mad.
 
B

Bjoern Schliessmann

Hi all. I'm learning python these days. I'm going to use this
thread to post, from time to time, my annoyances with python. I
hope someone will clarify things to me where I have misunderstood
them.

Annoyances:

1. Underscores! What's the deal with that? Especially those double
underscores. The best answer I read on this is that the double
underscores denotes special methods that the interpreter may
automatically use.

That's right. But what's the annoyance here?

It's a clear seperation. A bigger annoyance would be if the method
name were, e. g., "add". Now you define your own class, don't think
about this and define a method "add" yourself, and you wonder why
your class behaves strangely.
2. There are modules, there are functions, and there are classes-
methods!

You forgot static methods and types. And all of them are objects.
Wouldn't it have been easier had everything either been a
function or a class method?

IMHO no (this isn't Java). Why should I have to define a class if I
just code a little script with a few functions? That would be
forcing the programmer into using a programming paradigm.

Will you ask why there is no StringBuffer in Python? :)

Regards,


Björn
 
S

Steve Holden

Hi all. I'm learning python these days. I'm going to use this thread
to post, from time to time, my annoyances with python. I hope someone
will clarify things to me where I have misunderstood them.

Annoyances:

1. Underscores! What's the deal with that? Especially those double
underscores. The best answer I read on this is that the double
underscores denotes special methods that the interpreter may
automatically use. For example, 4+4 get expanded by the interpreter to
4.__add__(4).

2. There are modules, there are functions, and there are classes-
methods! Wouldn't it have been easier had everything either been a
function or a class method?
Actually, it's really simple. When you get right down to it,
*everything* in *every* current implementation of Python is either a one
or a zero. Would you like us to make it all zeros?

Perhaps you should meditate on the idea to the concept of "sufficient
and necessary complexity" ...

If you're a beginning programmer then it might be that Python contains
features you haven't yet needed. If you come from other languages (and
there's a certain Java-ish feel to the second comment) then perhaps you
aren't yet sufficiently familiar with Python to stop trying to write
other languages in it. I know I gave up Java because, among other
reasons, I found it tedious that I had to build a class method (and
reference it) when what I really wanted was a function.

Finally, I think as a learner you *can* do the language a service by
explaining what you find unnatural, inconvenient or incomprehensible. Do
please realise though that many times things will be as they are for
specific reasons, and sometimes even when the reasons aren't that good
some people will argue for retaining the status quo.

Your posting actually reads quite like a letter to Viz - see

http://www.viz.co.uk/

and follow the "Letterbocks" link - so it gave me a smile before I
realised it was (probably) serious.

regards
Steve
 
7

7stud

Annoyances:

Every language has annoyances. Python is no exception. Post away.
Anyone that is offended can go drink a Guinness.
1. Underscores! What's the deal with that? Especially those double
underscores. The best answer I read on this is that the double
underscores denotes special methods that the interpreter may
automatically use. For example, 4+4 get expanded by the interpreter to
4.__add__(4).

I thought those were pretty ugly myself. Now, I am used to them.
2. There are modules, there are functions, and there are classes-
methods! Wouldn't it have been easier had everything either been a
function or a class method?

I know what you mean. I always write:

someStringVar.len

and then I backspace and retype:

len(someString).

But then again, I can never remember whether length is a member or a
method in other languages.
 
M

Michael Hoffman

7stud said:
Every language has annoyances. Python is no exception. Post away.
Anyone that is offended can go drink a Guinness.

I find Guinness annoying.
 
F

flifus

That's right. But what's the annoyance here?

It's a clear seperation. A bigger annoyance would be if the method
name were, e. g., "add". Now you define your own class, don't think
about this and define a method "add" yourself, and you wonder why
your class behaves strangely.


You forgot static methods and types. And all of them are objects.


IMHO no (this isn't Java). Why should I have to define a class if I
just code a little script with a few functions? That would be
forcing the programmer into using a programming paradigm.

Will you ask why there is no StringBuffer in Python? :)

Regards,

Björn

Hi. You wrote c++, didn't you?

Well, why do some things in the library have to be functions, and
other things have to be class methods?

Why aren't they all just either functions or class methods? like
perhaps ruby.
 
M

Michael Hoffman

Well, why do some things in the library have to be functions, and
other things have to be class methods?

They don't have to be. They just are. That's like asking why do some
functions start with the letters a-m, and others with n-z. Why can't
they all begin with a-m? The answer would be that it would make the
language harder to use to cram concepts that should more naturally start
with n-z into spellings that start with a-m.
 
S

Steven Howe

Perhaps because some things are more naturally function like? For
'instance' (pardon the pun), functions shouldn't retain data. They
perform an operation, return data and quit. While retaining data is a
feature of an class/instance.

If I'm looking up the time of day in L.A., why do I need the whole clock
database of times including New York and London?

Another example are with respect to 'int' and 'float' operations. Why
should int(x) be a class? Did you want to save and operate on the value
of x again? No, you want the integer portion of x. Ditto float(x);
somebody input '5' but you want it clear it 5.0. You typecast it your
way. But the float operation doesn't need to retain the last input, nor
even exist after it's last use. So, without have any current values
referenced inside 'float', garbage collection can recover the memory
space of 'float'.

And before someone get's all technical, I know everything in Python is
an 'object' even None, which implies class, or is it the other way around?

sph
 
M

Marc 'BlackJack' Rintsch

Well, why do some things in the library have to be functions, and
other things have to be class methods?

Why aren't they all just either functions or class methods? like
perhaps ruby.

To which class should `sorted()` belong to then? Or the functions in the
`math` module? What about `itertools`?

In languages without functions, like Java, you'll have to write static
methods where you really want functions, just because Java forces you to
stuff everything into classes.

And instead of a simple ``lambda`` function one needs to write an
anonymous class with a method. Quite convoluted.

Ciao,
Marc 'BlackJack' Rintsch
 
N

Neil Cerutti

Perhaps because some things are more naturally function like?
For 'instance' (pardon the pun), functions shouldn't retain
data. They perform an operation, return data and quit. While
retaining data is a feature of an class/instance.

Functions do retain data. Classes and instances are just a
convenient notation. ;)
.... return lambda: f(x)
....10

In addition, all functions in Python have data members, too.
'd'

Python's scoping rules make certain kinds of functional idioms
hard to use, though. I'm not sure how to get the following to
work in Python using functions:
.... b = s
.... def add(a):
.... b += a
.... def balance():
.... return b
.... return add, balance
....Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 4, in add
UnboundLocalError: local variable 'b' referenced before assignment

Anyhow, it doesn't work, but you can see how closely it resembles
a class definition.
And before someone get's all technical, I know everything in
Python is an 'object' even None, which implies class, or is it
the other way around?

Classes are just objects like everything else.
 
K

Kay Schluehr

Well, why do some things in the library have to be functions, and
other things have to be class methods?

Why aren't they all just either functions or class methods? like
perhaps ruby.

A good question. Part of the answer might be that their are no
abstract base classes / interfaces in the language so far. These have
a particular meaning and you might ask whether some object has certain
abilities like being "sizable". With ABCs or interfaces it feels
natural to implement abstract methods in subclasses. Without them you
can either add methods ad hoc, without any conceptual background and
consistency requirements of your model, or you are going to implement
interface constraints in terms of so called "protocols". A protocol is
a sequence of actions which require interfaces to be present ( they
are abstract algorihms / command patterns ). Special functions
( __add__, __len__, etc. ) are the mediators of protocols. For
instance the meaning of len() can be understood in terms of protocols:

def len(obj):
return obj.__len__()

Other protocols are more implicit e.g. those that couple operators to
method calls. You already mentioned special methods for arithmetic
operations. I sometimes like to think that Python is a language
developed around protocols, and they have a strong backing in the
language. With Py3K Python will become a bit more Java like, at least
with regard to the supported language constructs.

Kay
 
K

Kay Schluehr

Well, why do some things in the library have to be functions, and
other things have to be class methods?

Why aren't they all just either functions or class methods? like
perhaps ruby.

A good question. Part of the answer might be that their are no
abstract base classes / interfaces in the language so far. These have
a particular meaning and you might ask whether some object has certain
abilities like being "sizable". With ABCs or interfaces it feels
natural to implement abstract methods in subclasses. Without them you
can either add methods ad hoc, without any conceptual background and
consistency requirements of your model, or you are going to implement
interface constraints in terms of so called "protocols". A protocol is
a sequence of actions which require interfaces to be present ( they
are abstract algorihms / command patterns ). Special functions
( __add__, __len__, etc. ) are the mediators of protocols. For
instance the meaning of len() can be understood in terms of protocols:

def len(obj):
return obj.__len__()

Other protocols are more implicit e.g. those that couple operators to
method calls. You already mentioned special methods for arithmetic
operations. I sometimes like to think that Python is a language
developed around protocols, and they have a strong backing in the
language. With Py3K Python will become a bit more Java like, at least
with regard to the supported language constructs.

Kay
 
F

Fuzzyman

Hi all. I'm learning python these days. I'm going to use this thread
to post, from time to time, my annoyances with python. I hope someone
will clarify things to me where I have misunderstood them.

Annoyances:

1. Underscores! What's the deal with that? Especially those double
underscores. The best answer I read on this is that the double
underscores denotes special methods that the interpreter may
automatically use. For example, 4+4 get expanded by the interpreter to
4.__add__(4).

I can understand this one. I don't have a problem with this myself,
but a lot of people find the proliferation of underscores in Python
bewildering. All I can say about it is that you soon get used to it -
and the separation is a good enough reason for me.

Fuzzyman
http://www.voidspace.org.uk/ironpython/index.shtml
 
B

Bjoern Schliessmann

Hi. You wrote c++, didn't you?

Yes :) But I mostly don't anymore and ported my main project from
C++ to Python.
Well, why do some things in the library have to be functions, and
other things have to be class methods?

Easy. Some things abstractly operate on all kind of stuff
(called "overloaded" in other languages), while others are,
regarding their function, tied to a specific class.
Why aren't they all just either functions or class methods?

Easy. Not all code in Python operates on a class. But that's what
Python's class methods are for. Only.
like perhaps ruby.

If I were rude, I would ask now why you don't use ruby. But I bet
ruby has some annoyances ready for you too.

Regards,


Björn
 
P

Paul McGuire

Functions do retain data. Classes and instances are just a
convenient notation. ;)

Python's scoping rules make certain kinds of functional idioms
hard to use, though. I'm not sure how to get the following to
work in Python using functions:
def account(s):
... b = s
... def add(a):
... b += a
... def balance():
... return b
... return add, balance
...
add, balance = account(100)
balance() 100
add(5)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 4, in add
UnboundLocalError: local variable 'b' referenced before assignment
Anyhow, it doesn't work, but you can see how closely it resembles
a class definition.

Use the outfix closure operator, []:
... b =
... def add(a):
... b[0] += a
... def balance():
... return b[0]
... return add, balance
...
;)

Jean-Paul- Hide quoted text -

- Show quoted text -


Check out the Percent class I posted at
http://www.programmingforums.org/forum/f43-python/t12461-arithmetic-by-percentage.html#post123290.
It allows you to write code like:

discount = Percent(20)
origprice = 35.00
saleprice = origprice - discount

-- Paul
 
P

Paul McGuire

More samples from that thread:

fica = Percent(7)
fedtax = Percent(15)
medicare = Percent(3)
deductions = fica + fedtax + medicare
gross = 100000
net = gross - deductions
print net # answer: 75000

wholesale = 10
markup = Percent(35)
retail = wholesale + markup
print retail # answer: 13.5

yearlyApprec = Percent(8)
basis = 10000
newbasis = basis + yearlyApprec + yearlyApprec + yearlyApprec
print newbasis # answer: 12597.12

-- Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top