python - constraint list to take only integers -


I'm just trying to increase the list type to get an integer I Attachments , < Strong> setitem , but it does not work. Please give any indication please

  class linked_list (list): def __init __ (self): self.indx = 0 def next (self): self.indx + = 1 return self.__ GetItem __ (self indx) def last (self, indx): self.indx - = 1 return yourself .__ GetItem __ (self.indx) def __append __ (self, val): if isinstance no (val, integer): Exception increases ('Only integers are accepted') Super (linked_list, self) .__ attached__ (wal) def __getitem __ (self, indx): try: return super (linked_list, self) .__ GetItem __ (indx ) IndexError, excluding e R: StopIteration () def __setitem __ (self, key, val): if does not isinstance, then (Val, integer): exceptions ('only integers accepted' lift) Super (linked_list, self) .__ setitem__ (Key, val)    

list You can participate with less code by applying MutableSequence automatically below all the other list methods based on the five methods. Import from the MutableSequence class IntList (MutableSequence): def __init __ (self): super (IntList, self) .__ init __ () self._list = [] DEF __len __ ( Self): return lane (self._list) def __getitem __ (self, index): return self._list [index] def __setitem __ (self, index, value): if isinstance no (value, integer): increased writing error ( 'Only integers are accepted') self._list [index] = value def __ (self, index) __delitem: del self._list [index] def included (self, index (Value, integer): TypeError ('only accepted acceptors are allowed)' if not: self._list.insert (index, value)

flip side However, it is for example, provided by expansion method MutableSequence , able to override all list methods for calls Will not be __len __ and __ insert __ for each element once it overrides any display-sensitive methods E by and it may be modified prior to use more efficiently.

See examples of examples that use MutableSequence remaining method to implement the list methods.

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -