anonymous assignment

Y

Yves Dorfsman

Is there anyway to tell python I don't care about a value ?

Say I want today's year and day, I'd like to do something like:

import time
y, None, d, None, None, None, None = time.localtime()

I know you can't assign anything to None, but I'm sure you get what I mean,
a special keyword that means I don't care about this value. In this
particular case, there's got to be a better way than:

d = time.local()
y = d[0]
d = d[1]



Thanks.


Yves.
http://www.SollerS.ca
 
P

Paul Rubin

Yves Dorfsman said:
import time
y, None, d, None, None, None, None = time.localtime()

I know you can't assign anything to None, but I'm sure you get what I
mean, a special keyword that means I don't care about this value.

You can just use a variable name than you ignore. It's traditional to
use _ but it's not a special keyword, it's just a another variable name:

y, _, d, _, _, _, _, _, _ = time.localtime()
 
Y

Yves Dorfsman

Paul said:
You can just use a variable name than you ignore. It's traditional to
use _ but it's not a special keyword, it's just a another variable name:

y, _, d, _, _, _, _, _, _ = time.localtime()

But you still have have a variable that's using memory for nothing. I find
this unsatisfactory... I don't want to compare languages, but I find the
perl "undef" elegant.


Yves.
http://www.SollerS.ca
 
M

Marc 'BlackJack' Rintsch

But you still have have a variable that's using memory for nothing. I
find this unsatisfactory...

Get over it…

Or use `operator.itemgetter()`:

In [36]: operator.itemgetter(0, 2)(time.localtime())
Out[36]: (2008, 12)

:)

Ciao,
Marc 'BlackJack' Rintsch
 
A

Arnaud Delobelle

Yves Dorfsman said:
Is there anyway to tell python I don't care about a value ?

Say I want today's year and day, I'd like to do something like:

import time
y, None, d, None, None, None, None = time.localtime()

I know you can't assign anything to None, but I'm sure you get what I
mean, a special keyword that means I don't care about this value. In
this particular case, there's got to be a better way than:

d = time.local()
y = d[0]
d = d[1]

I use Paul Rubin's solution (which is frown upon by many:), but it's
true it would be nice for tuples to have something like an extract()
method:

y, d = time.localtime.extract(0, 2)

Where

mytuple.extract(i1, i2, i3...)

would mean:

tuple(mytuple for i in (i1, i2, i3...))

Or perhaps allow indexing by tuples:

mytuple[i1, i2, i3...]
 
A

Arnaud Delobelle

Yves Dorfsman said:
Is there anyway to tell python I don't care about a value ?
Say I want today's year and day, I'd like to do something like:
import time
y, None, d, None, None, None, None = time.localtime()
I know you can't assign anything to None, but I'm sure you get what I
mean, a special keyword that means I don't care about this value. In
this particular case, there's got to be a better way than:
d = time.local()
y = d[0]
d = d[1]

I use Paul Rubin's solution (which is frown upon by many:), but it's
true it would be nice for tuples to have something like an extract()
method:

y, d = time.localtime.extract(0, 2)

Where

    mytuple.extract(i1, i2, i3...)

would mean:

    tuple(mytuple for i in (i1, i2, i3...))

Or perhaps allow indexing by tuples:

    mytuple[i1, i2, i3...]


here is a very sophisticated implementation :)
... return tuple(seq for i in indices)
...(2008, 12)
 
M

Michele Simionato

there's got to be a better way than:

d = time.local()
y = d[0]
d = d[1]

Uses Python 2.6! ;)

Python 2.6a3 (r26a3:62861, May 12 2008, 11:41:56)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.time.struct_time(tm_year=2008, tm_mon=5, tm_mday=12, tm_hour=11,
tm_min=43, tm_sec=47, tm_wday=0, tm_yday=133, tm_isdst=1)(2008, 12)
 
G

Gabriel Genellina

there's got to be a better way than:

d = time.local()
y = d[0]
d = d[1]

Uses Python 2.6! ;)

Python 2.6a3 (r26a3:62861, May 12 2008, 11:41:56)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.time.struct_time(tm_year=2008, tm_mon=5, tm_mday=12, tm_hour=11,
tm_min=43, tm_sec=47, tm_wday=0, tm_yday=133, tm_isdst=1)(2008, 12)

No need of 2.6 - the above code works since Python 2.2 at least:

Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.2008

(but struct_time objects were printed as regular tuples)
 
T

Terry Reedy

here is a very sophisticated implementation :)
.... return tuple(seq for i in indices)
....(2008, 12)

===================
Or a generator version:

# 3.0
def extract(iterable, indexes):
# assume indexes are all legal
enext = enumerate(iterable).__next__
i,item = enext()
for index in indexes:
while i < index:
i,item = enext()
yield item

import time
print(list(extract(time.localtime(), (0,2))))

#prints [2008, 12]
 
Y

Yves Dorfsman

Gabriel said:
Uses Python 2.6! ;)

No need of 2.6 - the above code works since Python 2.2 at least:

Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.2008

(but struct_time objects were printed as regular tuples)

And not well documented !

Nice one ! Thanks.
 
Y

Yves Dorfsman

Ben said:
No, you have one extra unused name binding. The values that you don't
want to use have *already* been allocated by the time the above
statement is executed. Name binding doesn't copy the values, it merely
binds a name to them. There's no "variable" in the above statement.

But if this happens in the main part of your script, it could take a long
time before this binding disapear, therefore, the gc won't be able to clean
that one up. In this particular case, it doesn't really matter (small size),
but imagine in a case where we are talking of a list of list, with
potentially large element in the list.


Yves.
 
Y

Yves Dorfsman

Scott said:
Yves said:
... Sorry this was a typo (again :), I meant:
d = time.local()
y = d[0]
d = d[2]
Then:
y, d = list(time.localtime())[:4:2]

What is this ?
Could you point me to a document on this syntax ?

I've tried it, it works, but I don't understand how.


Thanks.

Yves.
 
Y

Yves Dorfsman

Marc said:
Get over it…

Than what's the point of wanting a better language if every time we run in
something that looks wrong, or something that could be done better we should
just "get over it" ?


Yves.
 
K

Kam-Hung Soh

Scott said:
Yves said:
... Sorry this was a typo (again :), I meant:
d = time.local()
y = d[0]
d = d[2]
Then:
y, d = list(time.localtime())[:4:2]

What is this ?
Could you point me to a document on this syntax ?

I've tried it, it works, but I don't understand how.


Thanks.

Yves.

See:

"Built-in Functions", "slice()",
http://docs.python.org/lib/built-in-funcs.html

l = range(10)
l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
l[:4] # first four elements
[0, 1, 2, 3]
l[::2] # every second element
[0, 2, 4, 6, 8]
l[:4:2] # every second element in the first four elements
[0, 2]
 
A

Arnaud Delobelle

Terry Reedy said:
here is a very sophisticated implementation :)
... return tuple(seq for i in indices)
...(2008, 12)

===================
Or a generator version:

# 3.0
def extract(iterable, indexes):
# assume indexes are all legal
enext = enumerate(iterable).__next__
i,item = enext()
for index in indexes:
while i < index:
i,item = enext()
yield item

import time
print(list(extract(time.localtime(), (0,2))))

#prints [2008, 12]


but extract('python', (3, 1)) won't work!
 
T

Terry Reedy

|
| >
| >
| > here is a very sophisticated implementation :)
| >
| >>>> def extract(indices, seq):
| > ... return tuple(seq for i in indices)
| > ...
| >>>> y, d = extract((0, 2), time.localtime())
| >>>> y, d
| > (2008, 12)
| >
| > ===================
| > Or a generator version:
| >
| > # 3.0
| > def extract(iterable, indexes):
| > # assume indexes are all legal

and in order, I wrote once, but seem to have erased in revising (frown)

| > enext = enumerate(iterable).__next__
| > i,item = enext()
| > for index in indexes:
| > while i < index:
| > i,item = enext()
| > yield item
| >
| > import time
| > print(list(extract(time.localtime(), (0,2))))
| >
| > #prints [2008, 12]
|
| but extract('python', (3, 1)) won't work!
 
M

Marc 'BlackJack' Rintsch

Than what's the point of wanting a better language if every time we run in
something that looks wrong, or something that could be done better we should
just "get over it" ?

That advice wasn't for every time something looks wrong but this
particular one. You can solve it with `operator.itemgetter()` but that
means importing another module and calling a function which looks much
more "heavy weight" to me than one reference to an unused object which
might go away at the end of the function anyway soon.

Ciao,
Marc 'BlackJack' Rintsch
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top