1

what it is the best way to accomplish the following, either subclassing tuple or some other trick?

region = ( "buffer", "region" )  
region.cmd = ( "kill", "mark" )
3
  • You would probably subclass, but note that region is a tuple, not a list. Feb 13, 2011 at 1:40
  • Are you reimplementing Emacs in Python?
    – jwg
    Jun 19, 2015 at 9:31
  • 1
    @jwg ha, no. This was to generate Dragon-to-emacs (speech to text) files.
    – Anycorn
    Jun 19, 2015 at 15:36

4 Answers 4

6

You can simply subclass tuple without modification and it will work. By subclassing a built-in class it gains the ability to have arbitrary properties assigned to it, like normal user-defined classes.

class Region(tuple):
    pass

region = Region(( "buffer", "region" ))
region.cmd = ( "kill", "mark" )
1
class Region(tuple):
    def __init__(self, *args):
        super(Region, self).__init__(self, *args)
        self.cmd = None


region = Region(("buffer", "region"))
region.cmd = ("kill", "mark")
0

I'm not sure what you're asking.

If you want nested data structure I'd probably use a dictionary instead of a tuple.

region = {
    "buffer" : ("kill", "mark"),
    "region" : ("kill", "mark")
}

Tuples are non mutable as well I believe.

0

Use a named tuple: http://docs.python.org/dev/library/collections.html#collections.namedtuple

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.