Imho, in the class above the assignment to instance fields does not contain much programming logic and therefore can be safely 'abstracted away' by the language itself with a syntax which would look something like this:
class Server(object): def __init__(self, @host, @port, @protocol, @bufsize, @timeout): pass
This would be equivalent to the first example above, yet it does not obfuscate the code in any way. Or does it? It does look much cleaner to me.
Of course, the ampersand is just an arbitrary choice and might have bad connotations for those who read it as 'take address of' but @ has some allusion to delegates which maybe is ok.
I am not an experienced programmer and I am not sure if this is necessarily a good idea, so I wanted to get some feedback from more experienced Pythonistas before submitting it elsewhere.
> Imho, in the class above the assignment to instance fields does not > contain much programming logic and therefore can be safely 'abstracted > away' by the language itself with a syntax which would look something > like this:
> This would be equivalent to the first example above, yet it does not > obfuscate the code in any way. Or does it? It does look much cleaner > to me.
> Of course, the ampersand is just an arbitrary choice and might have > bad connotations for those who read it as 'take address of' but @ has > some allusion to delegates which maybe is ok.
> I am not an experienced programmer and I am not sure if this is > necessarily a good idea, so I wanted to get some feedback from more > experienced Pythonistas before submitting it elsewhere.
If you search on this list, you will find that there has been *many* proposals to remove self (which, I realize is slightly different than what yo propose) and that the main argument can be summarized as "Explicit is better than implicit."
Personally, I like the idea you suggest, with the modification that I would use "." instead of "@", as in
class Server(object): def __init__(self, .host, .port, .protocol, .bufsize, .timeout): pass
If one goes back to the original idea instead, the decision of using automatic assignment should depend on the signature of the __init__ function. Here's an implementation (using "_" instead of "." as it would lead to a syntax error):
from functools import * from inspect import *
def autoassign(_init_): @wraps(_init_) def _autoassign(self, *args, **kwargs): argnames, _, _, _ = getargspec(_init_) for name, value in zip(argnames[1:], args): if name.startswith("_"): setattr(self, name[1:], value) _init_(self, *args, **kwargs)
baz = 3 1 2 Traceback (most recent call last): File "/Users/andre/CrunchySVN/branches/andre/src/tools_2k.py", line 24, in exec_code exec code in local_dict File "User's code", line 23, in <module> AttributeError: 'Test' object has no attribute 'baz'
Dustan wrote: >> Well, you save one or two lines per class. Not enough in my >> opinion.
> Are you referring to the alternate syntax or to the decorator? Either > way, you could be saving 4 or 5 or more lines, if you have enough > arguments.
OK, but then again, every decent IDE should give you the tools to write an automation for that. Not that I don't like the idea of auto-assignment, but, you know ...
"André" <andre.robe...@gmail.com> wrote in message
news:7dcc86da-6ed7-48ec-9a9e-ada5574ae06e@v17g2000hsa.googlegroups.com... If one goes back to the original idea instead, the decision of using automatic assignment should depend on the signature of the __init__ function. Here's an implementation (using "_" instead of "." as it would lead to a syntax error):
from functools import * from inspect import *
def autoassign(_init_): @wraps(_init_) def _autoassign(self, *args, **kwargs): argnames, _, _, _ = getargspec(_init_) for name, value in zip(argnames[1:], args): if name.startswith("_"): setattr(self, name[1:], value) _init_(self, *args, **kwargs)
baz = 3 1 2 Traceback (most recent call last): File "/Users/andre/CrunchySVN/branches/andre/src/tools_2k.py", line 24, in exec_code exec code in local_dict File "User's code", line 23, in <module> AttributeError: 'Test' object has no attribute 'baz' =================================
I think this version, with this name convention, is nice enough to possibly go in the stdlib if there were an appropriate place for it. Not sure where though. If there were a classtools module....
"André" <andre.robe...@gmail.com> writes: > Personally, I like the idea you suggest, with the modification that I > would use "." instead of "@", as in