How to program safely in Python

General discussions related to Python.

Moderators: KDoiron, ChrJim, mawe, python

How to program safely in Python

Postby sri1025 on Tue Feb 06, 2007 4:20 am

Hi,

I am new to Python but I have programmed in other programming languages such as Java before. I have already started to feel a few differences which I don't know whether they are good or bad.

1) Type Safety
Python is not type-safe, but Java is type-safe. Though it appears to be a cool feature while writing a program, how cool it is when you read the code? For example, the results from a database can only be stored in an object of type ResultSet in Java, but here it's not the case. And I am sensing a big problem here, do you guys think programming without type-safety is a problem with Python? I feel type-safety is kind of a safety-net while programming and to program without it is like swimming without a safety-net in a deep ocean.

2) No Variable Declaration Needed
This doesn't sound like a feature to me, in fact it scares me. OK, we don't have type-safety, but this I think will cause so many programming errors that will be very hard to figure out. Is there any way we could force all the variables to be declared before using? Like Option Explicit of Visual Basic 6.

3) Non Object-Oriented-ness
I have just started reading the Python manual and I saw a few chapters on Classes but we can also program without a class (I mean C style). Isn't functional programming an old-old-thing? And calling functions like set('abdbd') (without qualifying it with a class name) is very new to me and I am feeling very insecure.

4) More Runtime errors?
Well, I am not sure about this but I misspelled a variable name 'cursor' as 'curor' and I called the close() method on it. I was using PyDT plug-in for eclipse and my IDE didn't show me any errors (I thought it would detect) and eventually it failed at run-time. Isn't this terrible? If we had type-safety in Python I couldn't have started my problem without rectifying the error (misspelled variable name) instead it failed after executing half the code.

And moreover, even if spell my variable name correctly, not all the methods are shown on auto-complete by my IDE. For example, cursor.close() was not shown by my IDE (PyDT) in the auto-complete, but it is a valid method! This never happens with Java.


I am not accusing Python's design or the Python programming language, in fact I work at a place where Python is our KING but I am new to it and these are my first reactions after trying out the language for the last 2 days and I am very eager to program in a Pythonic way :wink: . I am soon going to start my work with Python for the first time, and already I have so many challenges in front of me.

I hope the Python gurus over here can clear my doubts, and very excited to be the newest kid on the block. :)

-- Srikanth
Last edited by sri1025 on Tue Feb 06, 2007 9:57 pm, edited 1 time in total.
sri1025
New Python User
New Python User
 
Posts: 19
Joined: Tue Feb 06, 2007 3:21 am

Postby ffao on Tue Feb 06, 2007 5:39 am

Most of your problems really seem to come because you're used to java.

"Non type-safetiness" is not that bad. The difference is that Python detects that now you need that variable to point to a string, not an int when you assign it. This isn't a problem with functions as long as you remember to raise an exception on incorrect types (And often your code itself will. Remember your example? The function to store database results will complain if you don't give it a ResultSet as a parameter, because it won't find where to store the data).

Variable declarations would reduce much of the readability and would be of no use. The fact is that, as python uses an interpreter, it only sees the code it's executing... I don't have experience with java, but I know C errors are ugly, unfortunately. I don't know why the programming errors will be hard to track on python, because python beautifully raises an exception (NameError: name 'foo' is not defined, on which you'll usually see a typo or then proceed to initialize it before the calling code, if you had forgot it)

I don't think Java's style of forcing object-orientedness is very helpful. It creates this illusion that functional programming is old, while it has a lot of uses (using a class to write a hello world program is just silly), it depends on what you want to do, and java creates this insecureness with functional programming. Good thing is -- python doesn't force you to use a paradigm like Java does; you can continue to write object oriented code.

Well, about 4, I must say you're right. Sometimes you're only going to find out typos when your program exits.

Just consider the advantages and disadvatages; You may come to love python!
ffao
Ultimate Python Hacker
Ultimate Python Hacker
 
Posts: 1325
Joined: Sat Nov 04, 2006 12:42 pm
Location: Brasil

Postby Dietrich on Tue Feb 06, 2007 2:55 pm

There are two groups of people in this world. The first group is willing to take a risk, inovate, create, dream and enjoy life at its fullest. The second group only follows the tried and proven, slow and deliberately, happy with themselves they go through a fairly dull life. My recommendation, do not use Python, you won't able to survive the ordeal and just fall off the horse!
A hole is mostly nothing!
User avatar
Dietrich
Python Heavy Programmer
Python Heavy Programmer
 
Posts: 387
Joined: Sun Sep 11, 2005 3:23 pm
Location: Ann Arbor

Postby ffao on Tue Feb 06, 2007 3:09 pm

If he programs 1 year with python and continues to prefer java that's his choice, but you're recommending him to be on the second group? That's a contradiction, you know...
ffao
Ultimate Python Hacker
Ultimate Python Hacker
 
Posts: 1325
Joined: Sat Nov 04, 2006 12:42 pm
Location: Brasil

Postby sri1025 on Tue Feb 06, 2007 8:48 pm

Dietrich wrote:There are two groups of people in this world. The first group is willing to take a risk, inovate, create, dream and enjoy life at its fullest. The second group only follows the tried and proven, slow and deliberately, happy with themselves they go through a fairly dull life. My recommendation, do not use Python, you won't able to survive the ordeal and just fall off the horse!


Thanks for your motivation! :)

I mentioned clearly that I am not cussing or abusing Python, I just wanted to know why it was designed in that way and I just posted my first reactions after learning the language for the last 2 days. I am going to work on Python and time will tell how far I am going to survive.
sri1025
New Python User
New Python User
 
Posts: 19
Joined: Tue Feb 06, 2007 3:21 am

Re: How to program safely in Python

Postby merlinemrys on Tue Feb 06, 2007 8:52 pm

sri1025 wrote:3) Non Object-Oriented-ness
I have just started reading the Python manual and I saw a few chapters on Classes but we can also program without a class (I mean C style). Isn't functional programming an old-old-thing? And calling functions like set('abdbd') (without qualifying it with a class name) is very new to me and I am feeling very insecure.

Well, at least for me this point was a feature. I have nearly no programming experience, and classes are still something I don't understand. But the things I would like to programm work fine. Probably, the code will have a nicer "look" with classes, but that has to wait until I understand the basics of python. If I would have been forced to understand classes first, probably I could never learn this language.
And for some things, maybe defining a class and everything might just be like an "overkill"? If it works without classes, what is wrong with it?
Last edited by merlinemrys on Tue Feb 06, 2007 8:53 pm, edited 1 time in total.
merlinemrys
New Python User
New Python User
 
Posts: 48
Joined: Tue Oct 03, 2006 11:54 pm

Postby sri1025 on Tue Feb 06, 2007 8:52 pm

And I would like to know one thing, what is the best Free and Open Source IDE available for Python? I am currently using PyDT plug-in for eclipse and it doesn't seem like a good enough IDE. Any other recommendations, if it's platform independent, it will be great.

-- Srikanth
sri1025
New Python User
New Python User
 
Posts: 19
Joined: Tue Feb 06, 2007 3:21 am

Postby merlinemrys on Tue Feb 06, 2007 8:58 pm

Maybe check this site?
merlinemrys
New Python User
New Python User
 
Posts: 48
Joined: Tue Oct 03, 2006 11:54 pm

Re: How to program safely in Python

Postby sri1025 on Tue Feb 06, 2007 9:28 pm

merlinemrys wrote:
sri1025 wrote:3) Non Object-Oriented-ness
I have just started reading the Python manual and I saw a few chapters on Classes but we can also program without a class (I mean C style). Isn't functional programming an old-old-thing? And calling functions like set('abdbd') (without qualifying it with a class name) is very new to me and I am feeling very insecure.

Well, at least for me this point was a feature. I have nearly no programming experience, and classes are still something I don't understand. But the things I would like to programm work fine. Probably, the code will have a nicer "look" with classes, but that has to wait until I understand the basics of python. If I would have been forced to understand classes first, probably I could never learn this language.
And for some things, maybe defining a class and everything might just be like an "overkill"? If it works without classes, what is wrong with it?


I can't think of programming in C simply because there are no classes. I think inheritance, polymorphism, and all the things related to classes are essential in modern day programming.

Why do we need classes? It's the best thing we have to put all the relationships of a set of data and their operations together in a single entity. For example, Let's think we have to put a few elements in a list and we also need to do some mathematical calculations. Now, think of creating it without using classes. You will have a big module/file with all the set of methods/functions.

Code: Select all
addToList()
removeFromList()
replaceInList()
addNumbers()
multiplyNumbers()
blah()
blah()...


In the above class we have to use methods like addToList() and addNumbers() to know exactly what we are doing.

Now, at its first look, this file/module looks good, fine, nothing wrong and as many people say "it works". But, it has two problems.

1) High Coupling
2) Less Cohesion

Now, think in terms of classes. You can see two people here, I mean two classes.

List and Math. Now, write the two classes.

Code: Select all
class List {
   add();
   remove();
   replace();
}


Code: Select all
class Math {
   add();
   multiply();
   divide();
}


Now, the method names are short, meaningful and cohesive. You know when you have to add something you have to look into the Math class and not some abc module/file. In 1970s people used to think in terms of statements. 1980s people used to think in terms of methods/functions. From late 1990s people are thinking in terms of classes and nowadays we all hear about Component Based programming (which is nothing but packaging in Java and Python I think) where you put group of related classes into a package/component. For example, you can put all the data structure related classes like List, Stack, Queue, etc into a package called, you know, DS (Data Structure). I think Microsoft calls the same thing an Assembly in .NET.

They all tend to solve one problem, managing complexity. And software is about managing complexity. :)

-- Srikanth
sri1025
New Python User
New Python User
 
Posts: 19
Joined: Tue Feb 06, 2007 3:21 am

Postby sri1025 on Tue Feb 06, 2007 10:00 pm

Dietrich wrote:There are two groups of people in this world. The first group is willing to take a risk, inovate, create, dream and enjoy life at its fullest. The second group only follows the tried and proven, slow and deliberately, happy with themselves they go through a fairly dull life. My recommendation, do not use Python, you won't able to survive the ordeal and just fall off the horse!


Sorry, I meant to say that I am excited to be the newest KID on the block instead I misspelled it as newest KIND on the block (in my first post). May be this is what provoked your reactions. See, another spelling mistake. :)

-- Srikanth
sri1025
New Python User
New Python User
 
Posts: 19
Joined: Tue Feb 06, 2007 3:21 am

Re: How to program safely in Python

Postby merlinemrys on Tue Feb 06, 2007 11:02 pm

sri1025 wrote:I can't think of programming in C simply because there are no classes. I think inheritance, polymorphism, and all the things related to classes are essential in modern day programming.

Well, I survived quite well without it until now...
I guess you are completely rigth with all you have written. But when I need to do math, I just use the functions I find:
- adding numbers by: result = a+b
- multiplying numbers by: result = a*b
- adding to a list: result_list = old_list + new_element
- remove from a list: ehh... never needed that until now, maybe result_list = old_list - that_element will work?

That works fine, I find it quite easy to understand (looks like school math, for me), so - why should I use classes? (I guess that in fact I use classes because they are somewhere hidden behind the "+" and "*" signs. But I don't need to understand that at the moment.) Surely, if I would like to do more advanced things, I have to learn and to understand, but that's not my point. My point is: If there are things that work well enough without classes - why not doing things as easy as could be? You have written: "And software is about managing complexity." Why not avoid complexity wherever it is possible? For everything else Python have classes. You just don't have to use them, if it is not necessary. And to me, avoiding unnecessary complexity seems even better than any managment... (But maybe I'm just not ambitious enough to use the most difficult way I have a choice... :-o )
merlinemrys
New Python User
New Python User
 
Posts: 48
Joined: Tue Oct 03, 2006 11:54 pm

Re: How to program safely in Python

Postby sri1025 on Tue Feb 06, 2007 11:16 pm

merlinemrys,

I think we are on the same page. We are right in our thinking but may be I should have given a better example that add(), divide(). May be sinh(), cosh(), etc. And may be Python is Object Oriented wherever needed.

Thanks for a nice debate, I mean in a good sense!

-- Srikanth
sri1025
New Python User
New Python User
 
Posts: 19
Joined: Tue Feb 06, 2007 3:21 am

Postby Tantalum on Wed Feb 07, 2007 8:27 am

sri1025 wrote:And I would like to know one thing, what is the best Free and Open Source IDE available for Python? I am currently using PyDT plug-in for eclipse and it doesn't seem like a good enough IDE. Any other recommendations, if it's platform independent, it will be great.

-- Srikanth


There is a thread in the tool section that talks about the different editors people use.

MHO: I know both Java and Python and when I write programs in Java I think in a completly different way than I do when I program in Python. For some reason I'm under the illusion that Java is a huge beast of a programmiong language and programs written in Java should be enterprisy so when I write programs in Java I do every thing by the book (Use interfaces and slice everything into it's own package and class etc etc) and dont pay that much attention to how the program can be smaller/faster. On the other hand Python to me is a cool scripting language and when I write a program in it I care about writting the best program to solve the problem at hand with the least ammount of code and resource consumption without interfacing and classifying
Tantalum
Ultimate Python Hacker
Ultimate Python Hacker
 
Posts: 1117
Joined: Fri Mar 03, 2006 2:10 pm
Location: Where ever the voices in my head tell me to go

Postby sri1025 on Wed Feb 07, 2007 9:05 am

Thanks Tantalum,

I started to think in the same way and now I have started to enjoy Python. I shouldn't have compared these two languages but it happened automatically in my mind. But I need to have a different mindset when I code in Python as you do.

-- Srikanth
sri1025
New Python User
New Python User
 
Posts: 19
Joined: Tue Feb 06, 2007 3:21 am

Re: How to program safely in Python

Postby merlinemrys on Wed Feb 07, 2007 11:48 pm

sri1025 wrote:Thanks for a nice debate, I mean in a good sense!

It was a pleasure for me.

Thanks from Merlin!
merlinemrys
New Python User
New Python User
 
Posts: 48
Joined: Tue Oct 03, 2006 11:54 pm


Return to Python General

Who is online

Users browsing this forum: No registered users and 1 guest


Sponsored by Dreamlink Web hosting and Traduzioni Rumeno Italiano and ASSP Deluxe for cPanel.