(Python) Function calls for beginners

September 28, 2007

A function `call` is a callable object with a series of arguments which can be empty.

syntax : primary(arg_list['',''])
arg_list ::= positional_args [, keyword_ags][, *expression][, ** expression] |
keyword_args[, *expression][, ** expression] |
*expression [, **expression] |
** expression

positional_args ::= expression (, expression*)
keywords_args ::= keyword_item (, keyword_item*)
keyword_item ::= identifier = expression

`primary` is the callable object (function name, built-in callable method objs, class objs, methods of class instances and callable class instances)

How arguments are processed ?

1. a list of unfilled slots for formal parameters is created
2. N positional_args are placed in first N slots
3. keyword_args are placed in corrsponding slot using identifier

TypeError if a slot is already filled

4. Unfilled slots are filled with corresponding default values

TypeError if slots exist with no default values
TypeError if positional_args > formal param slots
TypeError if ketyword_args doesn’t match with formal param name unless **identifier is present

5. *expression evaluates to sequence like y1…yM and are placed as additional positional_args after x1…xN such that the call is made with x1….xN,y1..yM positional arguments
6. **expression is a dictionary contained excess keyword arguments

TypeError if **expression doesn’t evaluate to dictionary

7. *expression is processed before keyword arguments, hence unusual to use both keyword_args and *expression

A call return can be,

  • as per the return statement for a user-defined func
  • up to the interpreter for a build-in func or method
  • a new class instance for a class object
  • the user-defined function for a class instance method with the instance as the first argument
  • as per __call__() method for a class instance

Examples:

# define a function with two params, printing them as result
>>> def f(a,b): print a, b
...

# both are keyword_args
>>> f(a=1,b=2)
1 2

# both are positional_args
>>> f(1,2)
1 2

# a is keyword and b is considered positional. ERROR!
>>> f(a=1,2)
File "<stdin>", line 1
SyntaxError: non-keyword arg after keyword arg

# a is positional and b is keyword_arg
>>> f(1,b=2)
1 2

# 1 is positional while 2 is *expr and takes as additional positional_arg for b
>>> f(1,*(2,))
1 2

# 1 is keyword for a, but 2 is also positional_arg for a. ERROR!
>>> f(a=1,*(2,))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: f() got multiple values for keyword argument 'a'

# 1 is a keyword_arg for b, while 2 is positional_arg for a
>>> f(b=1,*(2,))
2 1

# 1 is keyword_arg for a, while 2 is specified thru **expr for b
>>> f(a=1,**{'b':2,})
1 2

# 1 is positional_arg using *expr for a, b is keyword_arg thru **expr for b
>>> f(*(1,),**{'b':2,})
1 2

# both a and b are specified as keyword_args thru **expression
>>> f(**{'a':1,'b':2,})
1 2

Original Reference (Python Reference Documentation)


chroot is not a security gate!

September 28, 2007

Happen to see a /. article about popular misconception of chroot being a security tool. I have heard a lot about chroot and how people generally use it as jail environments for security, but this article really contradicts that idea. The one who says is Alan Cox, so we need to get our eyes and ears a bit wide open :)


named my lappy

September 23, 2007

Since I saw a couple of posts in Planet Ubuntu about naming their computers, I wanted to name by lil laptop too. But this had been under procrastination because I could not find a satisfactory name as well as I remembered about it only when my laptop was not ON. Today I decided to change the name and went in search for the name. There were a lot of options, some funky names, mythical characters either from Indian mythology or from Roman/Greek and others. I decided to check Roman god names for a something interesting and finally settled with helios, the god of sun.

Now my laptop is named Helios, the god of sun who is the son of Hyperion and Theia. They say Helios sees and knows all. Upon dawn he rises in the East, driving his four horsed chariot, moving across the sky and descends at West in the night. Thus my laptop will be alive from dawn to dusk, seeing and knowing everything I subject it to :)


its obvious, they track you!

September 19, 2007

Today morning (a few minutes ago) I got an usual forward about Uncle Billy (you know who !?) sharing his fortune, blah blah blah! But this time, my eyes were wide open that I noted something which we all know and its obvious, but the innocent doze users fail to realize and that is..

When you forward this e-mail to friends, Microsoft can and will track it (if you are a Microsoft Windows user) for a two week time period.

So, they accept that M$ *can* and they *will* track (or rather crack into) doze machines and can take valuable user data ?! ;)

Unfortunately a lot of people who have believed this and forwarded it to all their friends (because each mail is said to fetch them $245) are from top MNCs of Indian IT arena. Thankfully am not a doze user (and hence no use in forwarding them, other than to /dev/null ;) )

At the end of the mail, it was said…

Within two week! s, Microsoft will contact you for your address and then send you a cheque.

Ohh, so they can’t/won’t get your address out of your machine ?! Or probably they will get your phone number and contact you ;) *evil grin* Probably they can get your account number and transfer you the money directly ;) *evil grin*

Update: I was pointed to an attachment with the forwarded mail, which I thought was part of the mail content, containing the following eye opening message for those who did forward them :P

According to the bogus email, these corporate giants are conducting an “e-mail beta test” to ensure that “Internet Explorer remains the most widely used program.”

Actually, all you will get is a sore trigger finger from pressing the Forward button and quite possibly return email from conscientous friends urging you to visit the Hoax Encyclopedia before acting on info found in an email.

I wonder how many in the “forward” army read that doc attachment with the forwarded mail. :(