Re: [web2py] Re: vars vs. kwargs
web2py makes extensive use of these. Take the A, DIV, or TABLE as examples, you can pass HTML attributes (e.g. _class, _id, _style) to these, but if you look at the code, you will notice that these things are not in the signature of these methods. They allow you to provide an unknown number of parameters to a method. For example: def my_method(text, _class='', _style='', _id='', _onclick='') Could easily be rewritten to: def my_method(text, **kwargs) Then kwargs is a dictionary with any extra parameters passed: if '_class' in kwargs: output += 'class = "%s"' % kwargs['_class'] Using *args on the other hand would be if you didn't expect named parameters to be used, just an unknown number of unnamed paramters. Hope that helps to clarify.
Re: [web2py] Re: vars vs. kwargs
pardon me, what is the meaning of *args and **vars? thank you.
[web2py] Re: vars vs. kwargs
fixed in trunk. Thanks On Jun 9, 9:24 am, Pawel Jasinski wrote: > hi, > sorry for being to cryptic. > > In [1]: def foo(*args,**kwargs): > ...: print vars > ...: > ...: > > In [2]: foo() > > > In [3]: def bar(*args,**vars): > ...: print vars > ...: > ...: > > In [4]: bar() > {} > > ref:http://docs.python.org/library/functions.html#vars > as I said, it is cosmetic. I believe it is a bad idea to give variable > a name which matches name of the build-in function > > cheers, > pawel > > On Thu, Jun 9, 2011 at 3:26 PM, Massimo Di Pierro > > > > > > > > wrote: > > I do not understand. :-( > > > On Jun 9, 3:21 am, Pawel Jasinski wrote: > >> hi, > >> it is cosmetic, but can be a pain for someone no so familiar with > >> python. > >> In examples for restful api: > > >> def index(): > >> def GET(*args,**vars): > >> patterns = [ > >> "/persons[person]", > >> "/{person.name.startswith}", > >> "/{person.name}/:field", > >> "/{person.name}/pets[pet.person]", > >> "/{person.name}/pet[pet.person]/{pet.name}", > >> "/{person.name}/pet[pet.person]/{pet.name}/:field" > >> ] > >> parser = db.parse_as_rest(patterns,args,vars) > >> if parser.status == 200: > >> return dict(content=parser.response) > >> else: > >> raise HTTP(parser.status,parser.error) > >> def POST(table_name,**vars): > >> if table_name == 'person': > >> return db.person.validate_and_insert(**vars) > >> elif table_name == 'pet': > >> return db.pet.validate_and_insert(**vars) > >> else: > >> raise HTTP(400) > >> return locals() > > >> after copy paste everything is ok, but the problem is waiting to > >> happen ... > >> vars hides built-in vars function. > > >> cheers, > >> pawel
[web2py] Re: vars vs. kwargs
Now I understand. It is a problem witg the example. Will fix it. On Jun 9, 9:24 am, Pawel Jasinski wrote: > hi, > sorry for being to cryptic. > > In [1]: def foo(*args,**kwargs): > ...: print vars > ...: > ...: > > In [2]: foo() > > > In [3]: def bar(*args,**vars): > ...: print vars > ...: > ...: > > In [4]: bar() > {} > > ref:http://docs.python.org/library/functions.html#vars > as I said, it is cosmetic. I believe it is a bad idea to give variable > a name which matches name of the build-in function > > cheers, > pawel > > On Thu, Jun 9, 2011 at 3:26 PM, Massimo Di Pierro > > > > > > > > wrote: > > I do not understand. :-( > > > On Jun 9, 3:21 am, Pawel Jasinski wrote: > >> hi, > >> it is cosmetic, but can be a pain for someone no so familiar with > >> python. > >> In examples for restful api: > > >> def index(): > >> def GET(*args,**vars): > >> patterns = [ > >> "/persons[person]", > >> "/{person.name.startswith}", > >> "/{person.name}/:field", > >> "/{person.name}/pets[pet.person]", > >> "/{person.name}/pet[pet.person]/{pet.name}", > >> "/{person.name}/pet[pet.person]/{pet.name}/:field" > >> ] > >> parser = db.parse_as_rest(patterns,args,vars) > >> if parser.status == 200: > >> return dict(content=parser.response) > >> else: > >> raise HTTP(parser.status,parser.error) > >> def POST(table_name,**vars): > >> if table_name == 'person': > >> return db.person.validate_and_insert(**vars) > >> elif table_name == 'pet': > >> return db.pet.validate_and_insert(**vars) > >> else: > >> raise HTTP(400) > >> return locals() > > >> after copy paste everything is ok, but the problem is waiting to > >> happen ... > >> vars hides built-in vars function. > > >> cheers, > >> pawel
Re: [web2py] Re: vars vs. kwargs
hi, sorry for being to cryptic. In [1]: def foo(*args,**kwargs): ...: print vars ...: ...: In [2]: foo() In [3]: def bar(*args,**vars): ...: print vars ...: ...: In [4]: bar() {} ref: http://docs.python.org/library/functions.html#vars as I said, it is cosmetic. I believe it is a bad idea to give variable a name which matches name of the build-in function cheers, pawel On Thu, Jun 9, 2011 at 3:26 PM, Massimo Di Pierro wrote: > I do not understand. :-( > > On Jun 9, 3:21 am, Pawel Jasinski wrote: >> hi, >> it is cosmetic, but can be a pain for someone no so familiar with >> python. >> In examples for restful api: >> >> def index(): >> def GET(*args,**vars): >> patterns = [ >> "/persons[person]", >> "/{person.name.startswith}", >> "/{person.name}/:field", >> "/{person.name}/pets[pet.person]", >> "/{person.name}/pet[pet.person]/{pet.name}", >> "/{person.name}/pet[pet.person]/{pet.name}/:field" >> ] >> parser = db.parse_as_rest(patterns,args,vars) >> if parser.status == 200: >> return dict(content=parser.response) >> else: >> raise HTTP(parser.status,parser.error) >> def POST(table_name,**vars): >> if table_name == 'person': >> return db.person.validate_and_insert(**vars) >> elif table_name == 'pet': >> return db.pet.validate_and_insert(**vars) >> else: >> raise HTTP(400) >> return locals() >> >> after copy paste everything is ok, but the problem is waiting to >> happen ... >> vars hides built-in vars function. >> >> cheers, >> pawel
[web2py] Re: vars vs. kwargs
I do not understand. :-( On Jun 9, 3:21 am, Pawel Jasinski wrote: > hi, > it is cosmetic, but can be a pain for someone no so familiar with > python. > In examples for restful api: > > def index(): > def GET(*args,**vars): > patterns = [ > "/persons[person]", > "/{person.name.startswith}", > "/{person.name}/:field", > "/{person.name}/pets[pet.person]", > "/{person.name}/pet[pet.person]/{pet.name}", > "/{person.name}/pet[pet.person]/{pet.name}/:field" > ] > parser = db.parse_as_rest(patterns,args,vars) > if parser.status == 200: > return dict(content=parser.response) > else: > raise HTTP(parser.status,parser.error) > def POST(table_name,**vars): > if table_name == 'person': > return db.person.validate_and_insert(**vars) > elif table_name == 'pet': > return db.pet.validate_and_insert(**vars) > else: > raise HTTP(400) > return locals() > > after copy paste everything is ok, but the problem is waiting to > happen ... > vars hides built-in vars function. > > cheers, > pawel