Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setitem and getitem -- python

Tags:

I've created a python program that makes a vector. Now i want to set an item using a function__setitem__ and__getitem__. So for example, ifvector = Vec()andvector[3] = 26would change an empty vector to[0, 0, 0, 26]. I need to override the__getitem__and__setitem__ I've listed the code below, but i'm having troubles with the get and set functions. Any advice?

class Vec:     def __init__(self, length = 0):         self.vector = [0]*length      def __str__(self):         return '[{}]'.format(', '.join(str(i) for i in self.vector))         #This formats the output according to the homework.         #Adds '[' and ']' and commas between each 0      def __len__(self):         return len(self.vector)      def extend(self, newLen):         self.vector.append([0]*newLen)         return (', '.join(str(j) for j in self.vector))      def __setitem__(self, key, item):         self.vector[key] = item      def __getitem__(self, key):         return self.vector[key] 
like image 471
user3014014 Avatar asked Dec 03 '13 16:12

user3014014


People also ask

What is Getitem and Setitem in Python?

__getitem__ and __setitem__ in Python They are predefined methods that simplify many operations that can be performed on a class instance, like __init__(), __str__(), __call__() etc. These methods are very helpful because they are used in binary operations, assignment operations, unary and binary comparison operations.

What is __ Getitem __ in Python?

__getitem__() is a magic method in Python, which when used in a class, allows its instances to use the [] (indexer) operators. Say x is an instance of this class, then x[i] is roughly equivalent to type(x). __getitem__(x, i) .

What is __ contains __ in Python?

Python string __contains__() is an instance method and returns boolean value True or False depending on whether the string object contains the specified string object or not. Note that the Python string contains() method is case sensitive.

What is the __ str __ method in Python?

Python __str__() This method returns the string representation of the object. This method is called when print() or str() function is invoked on an object. This method must return the String object.


1 Answers

You have several problems:

  1. extend is appending essentially a new vector to the end of the original, rather than increasing the length of the original. It's not clear that it needs to return a string representation of the modified vector (unless it's just for debugging purposes).

    def extend(self, newlength):     # Assume newlength is greater than the old     self.vector.extend([0] * (newlength - len(self))) 
  2. __setitem__ needs to call extend if the key is too large.

    def __setitem__(self, key, item):     if key >= len(self):         self.vector.extend(key+1)     self.vector[key] = item 
  3. __getitem__ needs to access the underlying list, not use an undefined attribute

    def __getitem__(self, key):     # It's probably better to catch any IndexError to at least provide     # a class-specific exception     return self.vector[key] 
like image 115
chepner Avatar answered Sep 17 '22 15:09

chepner