can anyone help me to figure out why the first code works and second dont?:
Working code
alias = []
alias.append(cx_Oracle.connect('%s/%s@%s' % (username, password, base)))
solve(*alias, **binds)
def solve(*alias, **binds):
cur = alias[0].cursor()
Not working code
alias = []
alias.append(cx_Oracle.connect('%s/%s@%s' % (username, password, base)))
solve(alias, binds)
def solve(*alias, **binds):
cur = alias[0].cursor()
Error
AttributeError: 'list' object has no attribute 'cursor'
*** Thanks all! That was very helpfull!
This is described in Keyword Arguments **binds
, Arbitrary Argument Lists *alias
and Unpacking Argument Lists solve(alias, binds)
vs solve(*alias, **binds)
.
When you call solve(alias, binds)
, both arguments alias
and binds
are assigned to the formal parameter *alias
.
This results in alias[0]
being the list [ cx_Oracle.connect('%s/%s@%s' % (username, password, base)) ]
instead of just the first element cx_Oracle.connect('%s/%s@%s' % (username, password, base))
. And the list [...]
doesn't have an attribute 'cursor', as Python aptly complains.