Skip to main content
added 50 characters in body
Source Link

As others have mentioned, the repetition isn't bad, but in some cases a namedtuple can be a great fit for this type of issue. This avoids using locals() or kwargs, which are usually a bad idea.

from collections import namedtuple
# declare a new object type with three properties; x y z
# the first arg of namedtuple takesis a typename
# the second arg is comma-separated or space-separated property names
XYZ = namedtuple("xyz""XYZ", "x, y, z")

# create an object of type XYZ. properties are in order
xyzabc = XYZ("one", "two", 3)
print xyzabc.x
print xyzabc.y
print xyzabc.z

I've found limited use for it, but you can inherit a namedtuple as with any other object (example continued):

class MySuperXYZ(XYZ):
    """ I add a helper function which returns the original properties """
    def properties(self):
        return self.x, self.y, self.z

xyz2abc2 = MySuperXYZ(4, "five", "six")
print xyz2abc2.x
print xyz2abc2.y
print xyz2abc2.z
print xyz2abc2.properties()

As others have mentioned, the repetition isn't bad, but in some cases a namedtuple can be a great fit for this type of issue. This avoids using locals() or kwargs, which are usually a bad idea.

from collections import namedtuple
# declare a new object type with three properties; x y z
# namedtuple takes comma-separated or space-separated property names
XYZ = namedtuple("xyz", "x, y, z")

# create an object of type XYZ. properties are in order
xyz = XYZ("one", "two", 3)
print xyz.x
print xyz.y
print xyz.z

I've found limited use for it, but you can inherit a namedtuple as with any other object (example continued):

class MySuperXYZ(XYZ):
    """ I add a helper function which returns the original properties """
    def properties(self):
        return self.x, self.y, self.z

xyz2 = MySuperXYZ(4, "five", "six")
print xyz2.x
print xyz2.y
print xyz2.z
print xyz2.properties()

As others have mentioned, the repetition isn't bad, but in some cases a namedtuple can be a great fit for this type of issue. This avoids using locals() or kwargs, which are usually a bad idea.

from collections import namedtuple
# declare a new object type with three properties; x y z
# the first arg of namedtuple is a typename
# the second arg is comma-separated or space-separated property names
XYZ = namedtuple("XYZ", "x, y, z")

# create an object of type XYZ. properties are in order
abc = XYZ("one", "two", 3)
print abc.x
print abc.y
print abc.z

I've found limited use for it, but you can inherit a namedtuple as with any other object (example continued):

class MySuperXYZ(XYZ):
    """ I add a helper function which returns the original properties """
    def properties(self):
        return self.x, self.y, self.z

abc2 = MySuperXYZ(4, "five", "six")
print abc2.x
print abc2.y
print abc2.z
print abc2.properties()
Source Link

As others have mentioned, the repetition isn't bad, but in some cases a namedtuple can be a great fit for this type of issue. This avoids using locals() or kwargs, which are usually a bad idea.

from collections import namedtuple
# declare a new object type with three properties; x y z
# namedtuple takes comma-separated or space-separated property names
XYZ = namedtuple("xyz", "x, y, z")

# create an object of type XYZ. properties are in order
xyz = XYZ("one", "two", 3)
print xyz.x
print xyz.y
print xyz.z

I've found limited use for it, but you can inherit a namedtuple as with any other object (example continued):

class MySuperXYZ(XYZ):
    """ I add a helper function which returns the original properties """
    def properties(self):
        return self.x, self.y, self.z

xyz2 = MySuperXYZ(4, "five", "six")
print xyz2.x
print xyz2.y
print xyz2.z
print xyz2.properties()