how to break tuple to separate variables

S

Stano Paska

Hi.

I need pass variables to function like tuple, but function accepts
separate variables.

Is there some elegant solution?

My example:

# -------------------------------
import datetime

def date2tuple(aaa):
try:
y = int(aaa[:4])
m = int(aaa[5:7])
d = int(aaa[8:10])
except:
y = m = d = 0
return (y, m, d)

# i need some like this
bbb = datetime.date(date2tuple('2004-11-03'))

# but date requires
bbb = datetime.date(2004, 11, 03)

# -------------------------------

Thanks.

Stano Paska
 
P

Paul Rubin

Stano Paska said:
# i need some like this
bbb = datetime.date(date2tuple('2004-11-03'))

# but date requires
bbb = datetime.date(2004, 11, 03)

use:

bbb = datetime.date(*date2tuple('2004-11-03'))

the '*' before the arg means convert the tuple to an arg list.
 
L

Larry Bates

Take a look at the built-in apply function:

apply(datetime.date, date2tuple('2004-11-03'))

Larry Bates
Syscon, Inc.
 
P

Peter Abel

Stano Paska said:
Hi.

I need pass variables to function like tuple, but function accepts
separate variables.

Is there some elegant solution?

My example:

# -------------------------------
import datetime

def date2tuple(aaa):
try:
y = int(aaa[:4])
m = int(aaa[5:7])
d = int(aaa[8:10])
except:
y = m = d = 0
return (y, m, d)

# i need some like this
bbb = datetime.date(date2tuple('2004-11-03'))

# but date requires
bbb = datetime.date(2004, 11, 03)

# -------------------------------

Thanks.

Stano Paska

try:

Regards
Peter
 
P

Peter Abel

Stano Paska said:
Hi.

I need pass variables to function like tuple, but function accepts
separate variables.

Is there some elegant solution?

My example:

# -------------------------------
import datetime

def date2tuple(aaa):
try:
y = int(aaa[:4])
m = int(aaa[5:7])
d = int(aaa[8:10])
except:
y = m = d = 0
return (y, m, d)

# i need some like this
bbb = datetime.date(date2tuple('2004-11-03'))

# but date requires
bbb = datetime.date(2004, 11, 03)

# -------------------------------

Thanks.

Stano Paska

Sorry, I didn't read your post seriously enough.[2004, 11, 3]

bbb = datetime.date(*map(int,'2004-11-03'.split('-')))
as Paul Rubin pointed out.

Regards
Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top