Reply to post: Re: 'programming styles from different languages are (to a varying extent) supported'

Python creator Guido van Rossum sys.exit()s as language overlord

JLV Silver badge

Re: 'programming styles from different languages are (to a varying extent) supported'

Different paradigms, rather than languages:

Functional - the list comprehensions and iterators, maps, reduces, lambdas, all sorts of goodies (which I know little about).

Object Oriented - pretty much everything is an object, including classes themselves and functions/methods. There's a lot of depth in the data model that few people use. For examples, classes are themselves objects with their class being a metaclass. That's useful, for some use cases, or for some people's programming preferences - I have a bit of a blind spot for them, they're an unused tool in my case. You can generate classes on the fly as well - say a class for each database table you are reading from.

(one thing to beware of : mutable objects as attributes, at a class level or as default arguments, bites everyone sooner or later. self._list = [] looks like cls._list = [], but in the first case appending stuff self._list.append(hit) affects your instance, in the second all that class's instances.)

Procedural - if you want to do write something with a main calling all sorts of functions, there's really nothing forcing you to use classes or objects as your building blocks - sticking to functions is perfectly permitted. ditto avoiding list comprehensions.

Multiplatform/scripting - rare is the case where you really have do worry about Windows vs Nix. os.path.join("foo","bar","zoom") will do the right thing on either, barring issues with Windows C:/D: drive names.

Since functions are objects, you can say assign any attribute, say a template to a function. The reasons why you might to do this are not common, but it can be helpful at times. For example, I explicitly assign template file paths to webserver functions because it allows you to automatically introspect which urls use which templates.

def f_view(**kwds):

(indent) print (f_view.template % kwds)

f_view.template = "my foo is: %(foo)s"

f_view(foo=2, bar="1")

All these tricks need to be sanity-checked against clarity - it is just as possible to write incomprehensible code in Python as in C!

POST COMMENT House rules

Not a member of The Register? Create a new account here.

  • Enter your comment

  • Add an icon

Anonymous cowards cannot choose their icon

Biting the hand that feeds IT © 1998–2019