Arrays, Got Me Confused

Richard Brodie R.Brodie at rl.ac.uk
Fri Apr 13 09:04:07 EDT 2007


"Robert Rawlins - Think Blue" <robert.rawlins at thinkbluemedia.co.uk> wrote in message 
news:mailman.6460.1176468727.32031.python-list at python.org...

> Wider fragments of code don't really exists at this moment in time
No but specifying the problem too narrowly tends to get you an
unidiomatic solution.

> Basically I'm trying to create a class that contains an array of MAC
> address, these look something like this 'FD:E4:55:00:FG:A9.

You rarely want to use 'array' in the standard library; there are some
use cases for it but they are rare. More often you want to use the
list type. However, here you really want to use a set: having
decided that, the code is so trivial, it's hardly worth making a new
class.

>>> s = set()
>>> s.add('FD:E4:55:00:FG:A9')
>>> s.remove('FD:E4:55:00:FG:A9')
>>> s = set()
>>> s.add('FD:E4:55:00:FG:A9')
>>> 'FD:E4:55:00:FG:A9' in s
True
>>> s.remove('FD:E4:55:00:FG:A9')
>>> 'FD:E4:55:00:FG:A9' in s
False
>>> s.clear()

Of course, you might want to add sanity checks like
'G' is not a hex digit in a real implementation.







More information about the Python-list mailing list