[BangPypers] Expressing meta-data about classes, methods, variables, parameters in python

Anand Balachandran Pillai abpillai at gmail.com
Thu Apr 3 12:52:28 CEST 2008


I had started a couple of blog posts aiming at discussing the new
features, changes and gotchas in Python 3.0. I had made the first
couple of posts 2 weeks back. The plan is to try and post as much
as possible before Py3k releases in Aug this year.

This question has reminded me to restart the posts. If someone
is interested checkout http://randombytes.blogspot.com .

Regards

--Anand

On Thu, Apr 3, 2008 at 4:15 PM, Anand Balachandran Pillai
<abpillai at gmail.com> wrote:
> Functions in Python are first-class objects, so you can add
>  arbitrary attributes to them like any other object.
>
>  Like Siddharth said, you can do,
>
>  def f(x, y): return x + y
>
>  f.x = 100
>  >>>f(20, 30)
>  50
>
>  However as of 2.x Python does not provide any syntax for formal function
>  annotations like Java. There have been proposals for this, and you
>  can find the most relevant one in the following PEP.
>
>  http://www.python.org/dev/peps/pep-3107/
>
>  This has been partly implemented and in py3k, functions have a new
>  attribute named "__annotations__" which is a dictionary which
>  can provide something more than a docstring, but an actual annotation
>  on the params, return values.
>
>  For example, this is how to annotate a function in Python 3.0.
>
>  >>> def f(x : "an integer", y: "another integer") -> "Sum of x and y":
>  ...     return x + y
>  >>> f.__annotations__
>  {'y': 'another integer', 'x': 'an integer', 'return': 'Sum of x and y'}
>
>  The help() function returns the annotation plus the docstring now.
>  >>> f.__doc__ = "A sum function"
>  >>> help(f)
>  Help on function f in module __main__:
>
>  f(x: 'an integer', y: 'another integer') -> 'Sum of x and y'
>     A sum function
>
>  All this is available in py3k, so check out the py3k trunk, build it and
>  play around with it.
>
>  Hope this helps.
>
>  --Anand
>
>
>
>
>  On Thu, Apr 3, 2008 at 2:26 PM, Sidharth Kuruvila
>  <sidharth.kuruvila at gmail.com> wrote:
>  > H Heshan,
>  >
>  >   Can you explain why you'd want to do this?
>  >
>  >  You can write
>  >
>  >  def f():
>  >      print f.a
>  >
>  >  f.a = 4
>  >
>  >  f()
>  >
>  >  But I can't see you would want to do something like that. Maybe if you
>  >  give a use case. Someone can come up with a solution.
>  >
>  >  Regards,
>  >  Sidharth
>  >
>  >
>  >
>  >  On Apr 3, 2008, at 2:03 PM, Heshan Suriyaarachchi wrote:
>  >
>  >  > Hi
>  >  > What I meant was a situation like this.
>  >  >
>  >  > In javascript I'm used to associating metadata with a function as
>  >  > follows.
>  >  >
>  >  > foo.bar = "foobar";
>  >  > function foo(){
>  >  > }
>  >  >
>  >  > What is the normal python practice to do this kind of a thing.
>  >  >
>  >  > Your help is very much appreciated.
>  >  >
>  >  > Thanks,
>  >  > Heshan Suriyaarachchi
>  >
>  >
>  > > _______________________________________________
>  >  > BangPypers mailing list
>  >  > BangPypers at python.org
>  >  > http://mail.python.org/mailman/listinfo/bangpypers
>  >
>  >  _______________________________________________
>  >  BangPypers mailing list
>  >  BangPypers at python.org
>  >  http://mail.python.org/mailman/listinfo/bangpypers
>  >
>
>
>
>  --
>  -Anand
>



-- 
-Anand


More information about the BangPypers mailing list