Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python code readability

Tags:

python

I have a programming experience with statically typed languages. Now writing code in Python I feel difficulties with its readability. Lets say I have a class Host:

class Host(object):
  def __init__(self, name, network_interface):
    self.name = name
    self.network_interface = network_interface

I don't understand from this definition, what "network_interface" should be. Is it a string, like "eth0" or is it an instance of a class NetworkInterface? The only way I'm thinking about to solve this is a documenting the code with a "docstring". Something like this:

class Host(object):
  ''' Attributes:
      @name: a string
      @network_interface: an instance of class NetworkInterface'''

Or may be there are name conventions for things like that?

like image 613
legesh Avatar asked Sep 11 '09 08:09

legesh


People also ask

Is Python created for code readability?

Python is a high-level, general-purpose programming language. Its design philosophy emphasizes code readability with the use of significant indentation.


1 Answers

Using dynamic languages will teach you something about static languages: all the help you got from the static language that you now miss in the dynamic language, it wasn't all that helpful.

To use your example, in a static language, you'd know that the parameter was a string, and in Python you don't. So in Python you write a docstring. And while you're writing it, you realize you had more to say about it than, "it's a string". You need to say what data is in the string, and what format it should have, and what the default is, and something about error conditions.

And then you realize you should have written all that down for your static language as well. Sure, Java would force you know that it was a string, but there's all these other details that need to be specified, and you have to manually do that work in any language.

like image 87
Ned Batchelder Avatar answered Sep 16 '22 19:09

Ned Batchelder