Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As mentioned above, if you want to keep things simple, just use a eg <code>_ordering</code> attribute, which manually keeps track of ordering. Otherwise, here is a metaclass approach (like the one Django uses), which creates an ordering attribute automatically.</p> <p><strong>Recording the original ordering</strong> </p> <p>Classes don't keep track of the ordering of the attributes. You can however keep track of which order the field instances were created. For that, you'll have to use your own class for fields (not int). The class keeps track of how many instances have already been made and each instance takes note of its position. Here is how you would do it for your example (storing integers):</p> <pre><code>class MyOrderedField(int): creation_counter = 0 def __init__(self, val): # Set the instance's counter, to keep track of ordering self.creation_counter = MyOrderedField.creation_counter # Increment the class's counter for future instances MyOrderedField.creation_counter += 1 </code></pre> <p><strong>Creating an <code>ordered_items</code> attribute automatically</strong></p> <p>Now that your fields have a number which can be used to order them, your parent class needs to use that somehow. You can do this a variety of ways, if I remember correctly, Django uses Metaclasses to do this, which is a bit wild for a simple class. </p> <pre><code>class BaseWithOrderedFields(type): """ Metaclass, which provides an attribute "ordered_fields", being an ordered list of class attributes that have a "creation_counter" attribute. """ def __new__(cls, name, bases, attrs): new_class = super(BaseWithOrderedFields, cls).__new__(cls, name, bases, attrs) # Add an attribute to access ordered, orderable fields new_class._ordered_items = [(name, attrs.pop(name)) for name, obj in attrs.items() if hasattr(obj, "creation_counter")] new_class._ordered_items.sort(key=lambda item: item[1].creation_counter) return new_class </code></pre> <p><strong>Using this metaclass</strong></p> <p>So, how do you use this? First, you need to use our new <code>MyOrderedField</code> class when defining your attributes. This new class will keep track of the order in which the fields were created:</p> <pre><code>class Ordered(object): __metaclass__ = BaseWithOrderedFields x = MyOrderedField(0) z = MyOrderedField(0) b = MyOrderedField(0) a = MyOrderedField(0) </code></pre> <p>Then you can access the ordered fields in our automatically created attribute <code>ordered_fields</code>:</p> <pre><code>&gt;&gt;&gt; ordered = Ordered() &gt;&gt;&gt; ordered.ordered_fields [('x', 0), ('z', 0), ('b', 0), ('a', 0)] </code></pre> <p>Feel free to change this to an ordered dict or just return the names or whatever you need. Additionally, you can define an empty class with the <code>__metaclass__</code> and inherit from there.</p> <p><strong>Don't use this!</strong></p> <p>As you can see, this approach is a little overcomplicated and probably not suitable for most tasks or python developers. If you're newish to python, you'll probably spend more time and effort developing your metaclass than you would have if you just defined the ordering manually. Defining your own ordering manually is almost always going to be the best approach. Django do it automatically because the complicated code is hidden from the end developer, and Django is used far more often than it itself is written/maintained. So only if you're developing a framework for other developers, then metaclasses may be useful for you.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload