473,386 Members | 1,644 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.

python newbie: some surprises

When I started coding in python, these two things surprised me.

1. my code is inconsistently indented with the combination of tabs and
spaces. Even lines looked intended, but it is not.

2. python requires to pass "self" to all instance methods

and I missed ":" often. :)
Jun 27 '08 #1
16 939
v4vijayakumar wrote:
When I started coding in python, these two things surprised me.

1. my code is inconsistently indented with the combination of tabs and
spaces. Even lines looked intended, but it is not.
Even the standard editor Idle tries to guess the intendation, so this
was never a problem for me. Though these days I use PyDev a lot.
2. python requires to pass "self" to all instance methods
A lot of editors help you with these 'unnecessary' things by
auto-completion might be worth looking into one.
>
and I missed ":" often. :)
Still do after 5 years of python abuse :-)

--
mph
Jun 27 '08 #2
On May 8, 2:06*am, v4vijayakumar <vijayakumar.subbu...@gmail.com>
wrote:
When I started coding in python, these two things surprised me.

1. my code is inconsistently indented with the combination of tabs and
spaces. Even lines looked intended, but it is not.
You must type inconsistently.

I never had such a problem even when I used to use Notepad.
>
2. python requires to pass "self" to all instance methods
Who uses methods?
>
and I missed ":" often. :)
Try using something like Seed7, where you have to use "then" with
"if" and "do" with "while" and "end" in every block. Maybe you'll
come to appreciate significant whitespace and ":".
Jun 27 '08 #3
Mensanator wrote:
>2. python requires to pass "self" to all instance methods

Who uses methods?
Is this a joke ?
What are the alternatives ?

>
>and I missed ":" often. :)

Try using something like Seed7, where you have to use "then" with
"if" and "do" with "while" and "end" in every block. Maybe you'll
come to appreciate significant whitespace and ":".
I see the point of the OP. Couldn't the new-line be used as an equivalent of
':', for example, do you find this difficult to read:

if a == 3
do_something()
if a == 3: do_something()
And surely, it should be easy to parse by the compiler.

Yves.
http://www.SollerS.ca
Jun 27 '08 #4
On May 8, 11:47�pm, Yves Dorfsman <y...@zioup.comwrote:
Mensanator wrote:
2. python requires to pass "self" to all instance methods
Who uses methods?

Is this a joke ?
Yes.
What are the alternatives ?
and I missed ":" often. :)
Try using something like Seed7, where you have to use "then" with
"if" and "do" with "while" and "end" in every block. Maybe you'll
come to appreciate significant whitespace and ":".

I see the point of the OP. Couldn't the new-line be used as an equivalent of
� ':', for example, do you find this difficult to read:

if a == 3
� �do_something()

if a == 3: do_something()

And surely, it should be easy to parse by the compiler.
If they were to chane it, I wouldn't complain.

I just think it doesn't deserve complaints when
compared to other systems.
>
Yves.http://www.SollerS.ca
Jun 27 '08 #5
En Fri, 09 May 2008 01:47:49 -0300, Yves Dorfsman <yv**@zioup.com>
escribió:
I see the point of the OP. Couldn't the new-line be used as an
equivalent of ':', for example, do you find this difficult to read:

if a == 3
do_something()
if a == 3: do_something()
And surely, it should be easy to parse by the compiler.
Yes, it could be done, there are no technical reasons to always force to
use ":". But AFAIK the main reasons to keep ":" are internal consistency
(an inner block always starts with ":"; incidentally, that's easier to
handle for editors) and legibility (the ":" stands for itself and has a
meaning)

--
Gabriel Genellina

Jun 27 '08 #6
v4vijayakumar a écrit :
When I started coding in python, these two things surprised me.

1. my code is inconsistently indented with the combination of tabs and
spaces. Even lines looked intended, but it is not.
Then you have a problem with your code editor - not with Python.
2. python requires to pass "self" to all instance methods
Nope. Python requires that function used as instance methods take the
instance as first argument (and that functions used as classmethods take
the class as first argument). It's the method object's duty to actually
pass the appropriate object to the function. The rational is that it
allows to built methods above two more generic constructs (namely:
functions and the descriptor protocol) instead of having to special-case
them.
and I missed ":" often. :)
Your editor should not indent the next line then. Either you failed to
correctly configure your editor, or it's broken.
Jun 27 '08 #7
Yves Dorfsman a écrit :
Mensanator wrote:
>>2. python requires to pass "self" to all instance methods

Who uses methods?

Is this a joke ?
Very probably.
What are the alternatives ?
Err... functions ?-)

Jun 27 '08 #8
Yves Dorfsman a écrit :
(snip)
I see the point of the OP. Couldn't the new-line be used as an
equivalent of ':',

Technically, yes. OTHO, the ':' helps editors doing proper indentation.
Jun 27 '08 #9
On May 9, 1:48 pm, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalidwrote:
v4vijayakumar a écrit :
When I started coding in python, these two things surprised me.
1. my code is inconsistently indented with the combination of tabs and
spaces. Even lines looked intended, but it is not.

Then you have a problem with your code editor - not with Python.
Editors can not be wrong. :)

I think there should be some way to say python compiler, to consider
tab and two blank spaces equal, when tab space = 2.
Jun 27 '08 #10
Gabriel Genellina wrote:
>I see the point of the OP. Couldn't the new-line be used as an
equivalent of ':', for example, do you find this difficult to read:

if a == 3
do_something()
if a == 3: do_something()

Yes, it could be done, there are no technical reasons to always force to
use ":". But AFAIK the main reasons to keep ":" are internal consistency
(an inner block always starts with ":"; incidentally, that's easier to
handle for editors) and legibility (the ":" stands for itself and has a
meaning)
Legibility ?
But one could make the same argument for curly brackets, and we seem to be
doing fine without them !

I have become so used to the power of indenting in python that I keep
forgetting the colon, and this is getting worse as I do more python, not
better. Maybe I'll write myself a "pre-compiler" that add the colons where
the compiler needs them :-)
Yves.
http://www.SollerS.ca

Jun 27 '08 #11
On Fri, 2008-05-09 at 15:08 +0000, Yves Dorfsman wrote:
Gabriel Genellina wrote:
I see the point of the OP. Couldn't the new-line be used as an
equivalent of ':', for example, do you find this difficult to read:

if a == 3
do_something()
if a == 3: do_something()
Yes, it could be done, there are no technical reasons to always force to
use ":". But AFAIK the main reasons to keep ":" are internal consistency
(an inner block always starts with ":"; incidentally, that's easier to
handle for editors) and legibility (the ":" stands for itself and has a
meaning)

Legibility ?
But one could make the same argument for curly brackets, and we seem to be
doing fine without them !

I have become so used to the power of indenting in python that I keep
forgetting the colon, and this is getting worse as I do more python, not
better. Maybe I'll write myself a "pre-compiler" that add the colons where
the compiler needs them :-)
Yves.
http://www.SollerS.ca

--
http://mail.python.org/mailman/listinfo/python-list
Have you considered the following:

if (x == 4 and (y in
[len(x) for x in
foo if x**2 23]
or y < 2) and z.strip().endswith('z') and
remove_first(w))
attach_list(q, r)
reject(x)

A colon on the correct line would help readability quite a bit.

Yeah, I know I made it pretty ugly to begin with, and there are ways to
improve it without the colon, but still, just because it could be
removed doesn't necessarily mean it should.

Cheers,
Cliff

Jun 27 '08 #12
En Fri, 09 May 2008 10:37:30 -0300, v4vijayakumar <vi******************@gmail.comescribió:
On May 9, 1:48 pm, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalidwrote:
>v4vijayakumar a écrit :
When I started coding in python, these two things surprised me.
1. my code is inconsistently indented with the combination of tabs and
spaces. Even lines looked intended, but it is not.

Then you have a problem with your code editor - not with Python.

Editors can not be wrong. :)

I think there should be some way to say python compiler, to consider
tab and two blank spaces equal, when tab space = 2.
It already considers tab = 8 spaces, and when invoked with -tt it rejects mixed tabs+spaces. (I would like Python rejected *any* tab used for indenting...)
There is a tool 'reindent.py' -somewhere on your Python install-, and an indentation checker 'tabnanny.py' (this one in the standard library).

--
Gabriel Genellina

Jun 27 '08 #13
Gabriel Genellina wrote:
En Fri, 09 May 2008 10:37:30 -0300, v4vijayakumar <vi******************@gmail.comescribió:
>On May 9, 1:48 pm, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalidwrote:
>>v4vijayakumar a écrit :

When I started coding in python, these two things surprised me.

1. my code is inconsistently indented with the combination of tabs and
spaces. Even lines looked intended, but it is not.

Then you have a problem with your code editor - not with Python.

Editors can not be wrong. :)

I think there should be some way to say python compiler, to consider
tab and two blank spaces equal, when tab space = 2.

It already considers tab = 8 spaces, and when invoked with -tt it rejects mixed tabs+spaces. (I would like Python rejected *any* tab used for indenting...)
There is a tool 'reindent.py' -somewhere on your Python install-, and an indentation checker 'tabnanny.py' (this one in the standard library).
That's one of the reasons why I like Python :-)

Still too many people don't know that you must set a TAB to 8 in
your editor. Anything other than 8 for a TAB will, at some point,
confuse somebody.

Don't confuse indentation with TAB setting.

Many editors are not helpfull either. Pydev, for example, has a setting
for TAB, but it is used for indentation. It is just luck (I think) that
pydev has an option to say that you only want spaces. (Take a look at
the main preferences of Pydev.)

So far, I have seen only one editor that understands the difference between
TABs and indentation, and that is Emacs.
--
Kees

Jun 27 '08 #14
Kees Bakker wrote:

So far, I have seen only one editor that understands the difference between
TABs and indentation, and that is Emacs.
Oh, well... in .vimrc:

autocmd FileType python set tabstop=8
autocmd FileType python set softtabstop=4
autocmd FileType python set expandtab

Jun 27 '08 #15
Kees Bakker <sp**@altium.nlwrote:
>
So far, I have seen only one editor that understands the difference between
TABs and indentation, and that is Emacs.
Most vi clones (and the original vi) do too! :-)

E.g. in the clone I use (vile) there are independent settings for
tabstop and shiftwidth. In addition you can tell the editor to change
tabs to spaces (or not) as you wish.

--
Chris Green
Jun 27 '08 #16
Lie
On May 8, 2:06*pm, v4vijayakumar <vijayakumar.subbu...@gmail.com>
wrote:
When I started coding in python, these two things surprised me.

1. my code is inconsistently indented with the combination of tabs and
spaces. Even lines looked intended, but it is not.
The problem is in tab not Python, there is no convention on how many
spaces should tab be treated, python (by default) assumes a tab to be
equal to 8 spaces, but not everyone thinks the same, some code editor
might consider it as 4 spaces or 2 spaces. In most python-oriented
code editor, a tab might be automatically replaced with four spaces.
2. python requires to pass "self" to all instance methods
No, python doesn't requires self to be passed to all instance methods,
python passes the "current instance" of a class to the first argument
of a function inside the class, this first argument can be named
anything, although it is traditionally named as self. In other
programming languages, this current instance is implicitly passed (Me
in VB, this in C/C++).
and I missed ":" often. :)
Not in your smiley though. :)
Jun 27 '08 #17

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

Similar topics

5
by: paolo veronelli | last post by:
I've a vague idea of the differences,I don't know scheme anyway. I'd like to see an example to show what is missing in python about closures and possibly understand if ruby is better in this...
114
by: Maurice LING | last post by:
This may be a dumb thing to ask, but besides the penalty for dynamic typing, is there any other real reasons that Python is slower than Java? maurice
22
by: James H. | last post by:
Greetings! I'm new to Python and am struggling a little with "and" and "or" logic in Python. Since Python always ends up returning a value and this is a little different from C, the language I...
30
by: Stuart Turner | last post by:
Hi Everyone, I'm working hard trying to get Python 'accepted' in the organisation I work for. I'm making some good in-roads. One chap sent me the text below on his views of Python. I wondered...
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: 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
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?
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:
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.