Over 10 Million Study Resources Now at Your Fingertips


Download as :
Rating : ⭐⭐⭐⭐⭐
Price : $10.99
Language:EN
Pages: 2

Method iadd calls singlelist method append

pythonhtp1_09.fm Page 325 Friday, December 14, 2001 2:01 PM

325

# add unique values from other
for value in other:

if value notin self:
list.append( self, value )

Fig. 9.12

Chapter 12, Exception Handling.]

Method __setitem__ (lines 26–33) executes when a client assigns a value to a par-ticular index. The method first calls utility method _raiseIfNotUnique with the value to insert. If the value already is in the list, the utility method raises an exception, method

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

Fig. 9.13

Inheriting from built-in type listfig09_13.py.

326

Object-Oriented Programming: Inheritance

Chapter 9

28
29
30
31
32
33
34
35
36

The value 2 appears 1 times in list
The value 5 appears 0 times in list
The index of 9 in the list is: 5
The value 4 was found in list

The list, after adding elements is: [1, 2, 3, 'hello', 4, 6, 9, 10, 20, -1, -2, -3, 100]

Fig. 9.13

Inheriting from built-in type listfig09_13.py.

Lines 36–44 overload the + operator for addition when a SingleList appears to the left or right of the operator. Methods __add__ and __radd__ each return a new object of class SingleList that is initialized with the elements of the two arguments passed to either method. This operation has the same effect as merging two lists into one list of

reference. Lines 55–62 overload the multiplication operation (i.e., list repetition) for

objects of class SingleList. By definition, a SingleList cannot contain more than one occurrence of any value, so method __mul__ raises an exception if the client attempts such an operation. Line 62 binds the names for methods __rmul__ (right multiplication) and __imul__ (augmented assignment multiplication) to the method defined for __mul__; when clients invoke these operations, the corresponding methods also raise exceptions.

How It Works
Login account
Login Your Account
Add to cart
Add to Cart
Payment
Make payment
Document download
Download File
PageId: ELI06A0C68
img
Uploaded by :
ewe5Gx7o
Page 1 Preview
method iadd calls singlelist method append
Sell Your Old Documents & Earn Wallet Balance