Python Constructors with Example

Python Constructors

Python Constructors

The first magic method we’ll take a look at is the constructor. In case you have never heard the word constructor before, it’s basically a fancy name for the kind of initializing method I have already used in some of the examples, under the name __init__.

 

What separates constructors from ordinary methods, however, is that the constructors are called automatically right after an object has been created. Thus, instead of doing what I’ve been doing up until now:

f = FooBar()

f.init()

constructors make it possible to simply do this:

>>> f = FooBar()

 

Creating constructors in Python is really easy; simply change the init method’s name from the plain old init to the magic version, __init__.

class FooBar:

def __init__(self):

self.somevar = 42

f = FooBar()

f.somevar

42

Now, that’s pretty nice. But you may wonder what happens if you give the constructor some parameters to work with. Consider the following:

class FooBar:

def __init__(self, value=42):

self.somevar = value

 

How do you think you could use this? Because the parameter is optional, you certainly could go on as nothing had happened. But what if you wanted to use it (or you hadn’t made it optional)? I’m sure you’ve guessed it, but let me show you anyway.

f = FooBar('This is a constructor argument')

f.somevar

'This is a constructor argument'

Of all the magic methods in Python, __init__ is quite certainly the one you’ll be using the most.

 

Note: Hae, are you interested in learning Python from scratch? Then Intellipaat Python Course is for you

 

Overriding Methods in General, and the Constructor in Particular

Each class may have one or more superclasses, from which they inherit behavior. If a method is called (or an attribute is accessed) on an instance of class B and it is not found, its superclass A will be searched. Consider the following two classes:

class A:

def hello(self):

print("Hello, I'm A.")

class B(A):

pass

Class A defines a method called hello, which is inherited by class B. Here is an example of how these classes work:

a = A()

b = B()

a.hello() Hello, I'm A.

b.hello() Hello, I'm A.

Because B does not define a hello method of its own, the original message is printed when hello is called. One basic way of adding functionality in the subclass is simply to add methods.

 

However, you may want to customize the inherited behavior by overriding some of the superclass’s methods. For example, it is possible for B to override the hello method. Consider this modified definition of B:

class B(A):

def hello(self):

print("Hello, I'm B.")

Using this definition, b.hello() will give a different result.

b = B()

b.hello() Hello, I'm B.

Overriding is an important aspect of the inheritance mechanism in general and may be especially important for constructors. Constructors are there to initialize the state of the newly constructed object, and most subclasses will need to have initialization code of their own, in addition to that of the superclass.

 

Even though the mechanism for overriding is the same for all methods, you will most likely encounter one particular problem more often when dealing with constructors than when overriding ordinary methods:

 

if you override the constructor of a class, you need to call the constructor of the superclass (the class you inherit from) or risk having an object that isn’t properly initialized.

 

Consider the following class, Bird:

class Bird:

def __init__(self):

self.hungry = True

def eat(self):

if self.hungry:

print('Aaaah ...')

self.hungry = False

else:

print('No, thanks!')

This class defines one of the most basic capabilities of all birds: eating. Here is an example of how you might use it:

b = Bird()

b.eat()

Aaaah ...

b.eat() No, thanks!

As you can see from this example, once the bird has eaten, it is no longer hungry. Now consider the subclass SongBird, which adds singing to the repertoire of behaviors.

class SongBird(Bird):

def __init__(self):

self.sound = 'Squawk!'

def sing(self):

print(self.sound)

The SongBird class is just as easy to use as Bird.

sb = SongBird()

sb.sing()

Squawk!

Because SongBird is a subclass of Bird, it inherits the eat method, but if you try to call it, you’ll discover a problem.

>>> sb.eat()

Traceback (most recent call last):

File "<stdin>", line 1, in ?

File "http://birds.py", line 6, in eat

if self.hungry:

AttributeError: SongBird instance has no attribute 'hungry'

The exception is quite clear about what’s wrong: the SongBird has no attribute called hungrily. Why should it? In SongBird, the constructor is overridden, and the new constructor doesn’t contain any initialization code dealing with the hungry attribute.

 

To rectify the situation, the SongBird constructor must call the constructor of its superclass, Bird, to make sure that the basic initialization takes place. There are basically two ways of doing this: by calling the unbound version of the superclass’s constructor or by using the super function. 

Superclass Constructor

Calling the Unbound Superclass Constructor

The approach described in this section is, perhaps, mainly of historical interest. With current versions of Python, using the super function (as explained in the following section) is clearly the way to go.

 

However, the much-existing code uses the approach described in this section, so you need to know about it. Also, it can be quite instructive—it’s a nice example of the difference between bound and unbound methods.

 

Now, let’s get down to business. If you find the title of this section a bit intimidating, relax. Calling the constructor of a superclass is, in fact, very easy (and useful). I’ll start by giving you the solution to the problem posed at the end of the previous section.

class SongBird(Bird):

def __init__(self):

Bird.__init__(self)

self.sound = 'Squawk!'

def sing(self):

print(self.sound)

Only one line has been added to the SongBird class, containing the code Bird.__init__(self). Before I explain what this really means, let me just show you that this really works.

sb = SongBird()

sb.sing()

Squawk!

sb.eat()

Aaaah ...

sb.eat() No, thanks!

But why does this work? When you retrieve a method from an instance, the self-argument of the method is automatically bound to the instance (a so-called bound method). You’ve seen several examples of that.

 

However, if you retrieve the method directly from the class (such as in Bird.__init__), there is no instance to which to bind. Therefore, you are free to supply any self you want to. 

 

By supplying the current instance as the self-argument to the unbound method, the songbird gets the full treatment from its superclass’s constructor (which means that it has its hungry attribute set).

super Function

Using the super Function

If you’re not stuck with an old version of Python, the super function is really the way to go. It works only with new-style classes, but you should be using those anyway.

 

It is called with the current class and instance as its arguments, and any method you call on the returned object will be fetched from the superclass rather than the current class.

 

So, instead of using Bird in the SongBird constructor, you can use super(SongBird, self). Also, the __init__ method can be called in a normal (bound) fashion. In Python 3, super can—and generally should—be called without any arguments and will do its job as if “by magic.”

 

The following is an updated version of the bird example:

class Bird:

def __init__(self):

self.hungry = True

def eat(self):

if self.hungry:

print('Aaaah ...')

self.hungry = False

else:

print('No, thanks!')

class SongBird(Bird):

def __init__(self):

super().__init__()

self.sound = 'Squawk!'

def sing(self):

print(self.sound)

This new-style version works just like the old-style one:

sb = SongBird()

sb.sing()

Squawk!

sb.eat()

Aaaah ...

sb.eat() No, thanks!

 

WHAT’S SO SUPER ABOUT SUPER?

In my opinion, the super function is more intuitive than calling unbound methods on the superclass directly, but that is not its only strength. The super function is actually quite smart, so even if you have multiple superclasses, you only need to use super once (provided that all the superclass constructors also use super).

multiple superclasses

Also, some obscure situations that are tricky when using old-style classes (for example, when two of your superclasses share a superclass) are automatically dealt with by new-style classes and super.

 

You don’t need to understand exactly how it works internally, but you should be aware that, in most cases, it is clearly superior to calling the unbound constructors (or other methods) of your superclasses.

 

So, what does super return, really? Normally, you don’t need to worry about it, and you can just pretend it returns the superclass you need. What it actually does is return a super object, which will take care of method resolution for you.

 

When you access an attribute on it, it will look through all your superclasses (and super-superclasses, and so forth) until it finds the attribute, or raises an AttributeError.

 

Item Access

Although __init__ is by far the most important special method you’ll encounter, many others are available to enable you to achieve quite a lot of cool things. One useful set of magic methods described in this section allows you to create objects that behave like sequences or mappings.

 

The basic sequence and mapping protocol is pretty simple. However, to implement all the functionality of sequences and mappings, there are many magic methods to implement. Luckily, there are some shortcuts, but I’ll get to that.

 

The Basic Sequence and Mapping Protocol

Sequences and mappings are basically collections of items. To implement their basic behavior (protocol), you need two magic methods if your objects are immutable, or four if they are mutable.

 

__len__(self):

This method should return the number of items contained in the collection. For a sequence, this would simply be the number of elements. For mapping, it would be the number of key-value pairs.

 

If __len__ returns zero (and you don’t implement __nonzero__, which overrides this behavior), the object is treated as false in a Boolean context (as with empty lists, tuples, strings, and dictionaries).

 

__getitem__(self, key):

This should return the value corresponding to the given key. For a sequence, the key should be an integer from zero to n–1 (or, it could be negative, as noted later), where n is the length of the sequence. For mapping, you could really have any kind of keys.

 

__setitem__(self, key, value): This should store the value in a manner associated with the key, so it can later be retrieved with __getitem__. Of course, you define this method only for mutable objects.

 

__delitem__(self, key): This is called when someone uses the __del__ statement on a part of the object and should delete the element associated with the key. Again, only mutable objects (and not all of them—only those for which you want to let items be removed) should define this method.

 

Some extra requirements are imposed on these methods.

  • For a sequence, if the key is a negative integer, it should be used to count from the end. In other words, treat x[-n] the same as x[len(x)-n].
  • If the key is of an inappropriate type (such as a string key used on a sequence), a TypeError may be raised.
  • If the index of a sequence is of the right type, but outside the allowed range, an IndexError should be raised.
  • For a more extensive interface, along with a suitable abstract base class (Sequence), consult the documentation for the collections module.

Subclassing list, dict, and str

 

If you want custom behavior in only one of the operations, it makes no sense that you should need to reimplement all of the others. It’s just programmer laziness (also called common sense).

 

So what should you do? The magic word is inheritance. Why reimplement all of these things when you can inherit them?

 

The standard library comes with abstract and concrete base classes in the collections module, but you can also simply subclass the built-in types themselves. So, if you want to implement a sequence type that behaves similarly to the built-in lists, you can simply subclass list.

 

Let’s just do a quick example—a list with an access counter.

class CounterList(list):

def __init__(self, *args):

super().__init__(*args)

self.counter = 0

def __getitem__(self, index):

self.counter += 1

return super(CounterList, self).__getitem__(index)

The CounterList class relies heavily on the behavior of its subclass superclass (list). Any methods not overridden by CounterList (such as append, extend, index, and so on) may be used directly.

 

In the two methods that are overridden, super is used to call the superclass version of the method, adding only the necessary behavior of initializing the counter attribute (in __init__) and updating the counter attribute (in __getitem__).

 

¦¦Note Overriding __getitem__ is not a bulletproof way of trapping user access because there are other ways of accessing the list contents, such as through the pop method.

 

Here is an example of how CounterList may be used:

cl = CounterList(range(10))

cl

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

cl.reverse()

cl

[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

del cl[3:6]

cl

[9, 8, 7, 3, 2, 1, 0]

cl.counter

0

cl[4] + cl[2]

9

cl.counter

2

As you can see, CounterList works just like the list in most respects. However, it has a counter attribute (initially zero), which is incremented each time you access a list element. After performing the addition cl[4] + cl[2], the counter has been incremented twice, to the value 2.

advanced users

More Magic

Special (magic) names exist for many purposes—what I’ve shown you so far is just a small taste of what is possible. Most of the magic methods available are meant for fairly advanced users.

 

However, if you are interested, it is possible to emulate numbers, make objects that can be called as if they were functions, influence how objects are compared, and much more. 

 

Properties

Accessors are simply methods with names such as getHeight and setHeight and are used to retrieve or rebind some attribute. Encapsulating state variables (attributes) like this can be important if certain actions must be taken when accessing the given attribute. For example, consider the following Rectangle class:

class Rectangle:

def __init__(self):

self.width = 0

self.height = 0

def set_size(self, size):

self.width, self.height = size

def get_size(self):

return self.width, self.height

Here is an example of how you can use the class:

r = Rectangle()

r.width = 10

r.height = 5

r.get_size() (10, 5)

r.set_size((150, 100))

r.width

150

The get_size and set_size methods are accessors for a fictitious attribute called size—which is simply the tuple consisting of width and height. 

 

This code isn’t directly wrong, but it is flawed. The programmer using this class shouldn’t need to worry about how it is implemented (encapsulation).

 

If you someday wanted to change the implementation so that size was a real attribute and width and height were calculated on the fly, you would need to wrap them in accessors, and any programs using the class would also have to be rewritten. The client code (the code using your code) should be able to treat all your attributes in the same manner.

 

So what is the solution? Should you wrap all your attributes in accessors? That is a possibility, of course.

 

However, it would be impractical (and kind of silly) if you had a lot of simple attributes because you would need to write many accessors that did nothing but retrieve or set these attributes, with no useful action taken.

 

This smells of copy-paste programming, or cookie-cutter code, which is clearly a bad thing (although quite common for this specific problem in certain languages).

 

Luckily, Python can hide your accessors for you, making all of your attributes look alike. Those attributes that are defined through their accessors are often called properties.

 

Python actually has two mechanisms for creating properties in Python. I’ll focus on the most recent one, the property function, which works only on new-style classes. Then I’ll give you a short description of how to implement properties with magic methods.

 

The property Function

property Function

Using the property function is delightfully simple. If you have already written a class such as a Rectangle from the previous section, you need to add only a single line of code.

class Rectangle:

def __init__ (self):

self.width = 0

self.height = 0

def set_size(self, size):

self.width, self.height = size

def get_size(self):

return self.width, self.height

size = property(get_size, set_size)

In this new version of Rectangle, a property is created with the property function with the accessor functions as arguments (the getter first, then the setter), and the name size is then bound to this property.

 

After this, you no longer need to worry about how things are implemented but can treat width, height, and size the same way.

r = Rectangle()

r.width = 10

r.height = 5

r.size

(10, 5)

r.size = 150, 100

r.width

150

As you can see, the size attribute is still subject to the calculations in get_size and set_size, but it looks just like a normal attribute.

 

In fact, the property function may be called with zero, one, three, or four arguments as well. If called without any arguments, the resulting property is neither readable nor writable.

 

If called with only one argument (a getter method), the property is readable only. The third (optional) argument is a method used to delete the attribute (it takes no arguments). 

 

Although this section has been short (a testament to the simplicity of the property function), it is very important. The moral is this: with new-style classes, you should use property rather than accessory.

Static Methods

Static Methods and Class Methods

Before discussing the old way of implementing properties, let’s take a slight detour and look at another couple of features that are implemented in a similar manner to the new-style properties.

Here is a simple example:

class MyClass:

def smeth():

print('This is a static method')

smeth = staticmethod(smeth)

def cmeth(cls):

print('This is a class method of', cls)

cmeth = classmethod(cmeth)

The technique of wrapping and replacing the methods manually like this is a bit tedious. In Python 2.4, a new syntax was introduced for wrapping methods like this, called decorators. 

 

They actually work with any callable objects as wrappers and can be used on both methods and functions. You specify one or more decorators (which are applied in reverse order) by listing them above the method (or function), using the @ operator.

class MyClass:

@staticmethod

def smeth():

print('This is a static method')

@classmethod

def cmeth(cls):

print('This is a class method of', cls)

Once you’ve defined these methods, they can be used like this (that is, without instantiating the class):

MyClass.smeth()

This is a static method

MyClass.cmeth()

This is a class method of <class '__main__.MyClass'>

 

Static methods and class methods haven’t historically been important in Python, mainly because you could always use functions or bound methods instead, in some way, but also because the support hasn’t really been there in earlier versions.

 

So even though you may not see them used much in current code, they do have their uses (such as factory functions, if you’ve heard of those), and you may well think of some new ones.

__getattr__, __setattr__, and Friends

 

It’s possible to intercept every attribute access on an object. Among other things, you could use this to implement properties with old-style classes (where property won’t necessarily work as it should).

 

To have code executed when an attribute is accessed, you must use a couple of magic methods. The following four provide all the functionality you need (in old-style classes, you only use the last three):

 

__getattribute__(self, name): Automatically called when the attribute name is accessed. (This works correctly on new-style classes only.)

__getattr__(self, name): Automatically called when the attribute name is accessed and the object has no such attribute.

__setattr__(self, name, value): Automatically called when an attempt is made to bind the attribute name to value.

__delattr__(self, name): Automatically called when an attempt is made to delete the attribute name.

Although a bit trickier to use (and in some ways less efficient) than property, these magic methods are quite powerful, because you can write code in one of these methods that deals with several properties. 

 

Here is the Rectangle example again, this time with magic methods:

class Rectangle:

def __init__ (self):

self.width = 0

self.height = 0

def __setattr__(self, name, value):

if name == 'size':

self.width, self.height = value

else:

self. __dict__[name] = value

def __getattr__(self, name):

if name == 'size':

return self.width, self.height

else:

raise AttributeError()

As you can see, this version of the class needs to take care of additional administrative details. When considering this code example, it’s important to note the following:

 

The __setattr__ method is called even if the attribute in question is not size. Therefore, the method must take both cases into consideration: if the attribute is size, the same operation is performed as before; otherwise, the magic attribute __dict__ is used.

 

It contains a dictionary with all the instance attributes. It is used instead of ordinary attribute assignment to avoid having __setattr__ called again (which would cause the program to loop endlessly).

 

The __getattr__ method is called only if a normal attribute is not found, which means that if the given name is not size, the attribute does not exist, and the method raises an AttributeError.

 

This is important if you want the class to work correctly with built-in functions such as hasattr and getattr. If the name is size, the expression found in the previous implementation is used.

 

¦¦Note Just as there is an “endless loop” trap associated with __setattr__, there is a trap associated with __getattribute__. Because it intercepts all attribute accesses (in new-style classes), it will intercept accesses to __dict__ as well!

 

The only safe way to access attributes on self inside __getattribute__ is to use the __getattribute__ method of the superclass (using super).

 

Iterators

Iterators

I’ve mentioned iterators (and iterables) briefly in preceding chapters. In this section, I go into some more detail. I cover only one magic method, __iter__, which is the basis of the iterator protocol.

 

The Iterator Protocol

To iterate means to repeat something several times—what you do with loops. Until now I have iterated over only sequences and dictionaries in for loops, but the truth is that you can iterate over other objects, too: objects that implement the __iter__ method.

 

The __iter__ method returns an iterator, which is an object with a method called __next__, which is callable without any arguments. When you call the __next__ method, the iterator should return its “next value.”

 

If the method is called and the iterator has no more values to return, it should raise a StopIteration exception. There is a built-in convenience function called next that you can use, where next(it) is equivalent to it.__next__().

 

¦¦Note The iterator protocol is changed a bit in Python 3. In the old protocol, iterator objects should have a method called next rather than __next__.

 

What’s the point? Why not just use a list? Because it may often be overkill. If you have a function that can compute values one by one, you may need them only one by one—not all at once, in a list, for example. If the number of values is large, the list may take up too much memory.

 

But there are other reasons: using iterators is more general, simpler, and more elegant. Let’s take a look at an example you couldn’t do with a list, simply because the list would need to be of infinite length!

 

Our “list” is the sequence of Fibonacci numbers. An iterator for these could be the following:

class Fibs:

def __init__(self):

self.a = 0

self.b = 1

def __next__(self):

self.a, self.b = self.b, self.a + self.b

return self.a

def __iter__(self):

return self

Note that the iterator implements the __iter__ method, which will, in fact, return the iterator itself. In many cases, you would put the __iter__ method in another object, which you would use in the for a loop.

 

That would then return your iterator. It is recommended that iterators implement an __iter__ method of their own in addition (returning self, just as I did here), so they themselves can be used directly in for loops.

 

First, make a Fibs object.

>>> fibs = Fibs()

You can then use it in a for loop—for example, to find the smallest Fibonacci number that is greater than 1,000.

for f in fibs:

...if f > 1000:

...print(f)

...break

...

1597

Here, the loop stops because I issue a break inside it; if I didn’t, the for loop would never end.

 

it = iter([1, 2, 3])

next(it)

1

next(it)

2

It can also be used to create an iterable from a function or other callable object.

 

Making Sequences from Iterators

In addition to iterating over the iterators and iterables (which is what you normally do), you can convert them to sequences.

Iterators

In most contexts in which you can use a sequence (except in operations such as indexing or slicing), you can use an iterator (or an iterable object) instead. One useful example of this is explicitly converting an iterator to a list using the list constructor.

class TestIterator:

...value = 0

...def __next__(self):

... self.value += 1

... if self.value > 10: raise StopIteration

... return self.value

... def __iter__(self):

... return self

...

ti = TestIterator()

list(ti)

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

 

Generators

Generators

Generators (also called simple generators for historical reasons) are relatively new to Python and are (along with iterators) perhaps one of the most powerful features to come along for years.

 

However, the generator concept is rather advanced, and it may take a while before it “clicks” and you see how it works or how it would be useful for you. Rest assured that while generators can help you write really elegant code, you can certainly write any program you wish without a trace of generators.

 

A generator is a kind of iterator that is defined with normal function syntax. Exactly how generators work is best shown through example. Let’s first take a look at how you make them and use them and then take a peek under the hood.

 

Making a Generator

Making a generator is simple; it’s just like making a function. I’m sure you are starting to tire of the good old Fibonacci sequence by now, so let me do something else. I’ll make a function that flattens nested lists. The argument is a list that may look something like this:

nested = [[1, 2], [3, 4], [5]]

 

In other words, it’s a list of lists. My function should then give me the numbers in order. Here’s a solution:

def flatten(nested):

for sublist in nested:

for element in sublist:

yield element

Most of this function is pretty simple. First, it iterates over all the sublists of the supplied nested list; then it iterates over the elements of each sublist in order. If the last line had been print(element), for example, the function would have been easy to understand, right?

 

So what’s new here is the yield statement. Any function that contains a yield statement is called a generator. And it’s not just a matter of naming; it will behave quite differently from ordinary functions.

 

The difference is that instead of returning one value, as you do with return, you can yield several values, one at a time.

 

Each time a value is yielded (with yield), the function freezes; that is, it stops its execution at exactly that point and waits to be reawakened. When it is, it resumes its execution at the point where it stopped.

 

I can make use of all the values by iterating over the generator.

nested = [[1, 2], [3, 4], [5]]

for num in flatten(nested):

...print(num)

...

1

2

3

4

5

or

list(flatten(nested)) [1, 2, 3, 4, 5]

A Recursive Generator

The generator I designed in the previous section could deal only with lists nested two levels deep, and to do that it used two for loops. What if you have a set of lists nested arbitrarily deeply?

 

Perhaps you use them to represent some tree structure, for example. (You can also do that with specific tree classes, but the strategy is the same.)

 

You need a for loop for each level of nesting, but because you don’t know how many levels there are, you must change your solution to be more flexible. It’s time to turn to the magic of recursion.

def flatten(nested):

try:

for sublist in nested:

for element in flatten(sublist):

yield element

except TypeError:

yield nested

When flatten is called, you have two possibilities (as is always the case when dealing with recursion): the base case and the recursive case.

 

In the base case, the function is told to flatten a single element (for example, a number), in which case the for loop raises a TypeError (because you’re trying to iterate over a number), and the generator simply yields the element.

 

If you are told to flatten a list (or any iterable), however, you need to do some work. You go through all the sublists (some of which may not really be lists) and call flatten on them. Then you yield all the elements of the flattened sublists by using another for a loop. It may seem slightly magical, but it works.

 

list(flatten([[[1], 2], 3, 4, [5, [6, 7]], 8])) [1, 2, 3, 4, 5, 6, 7, 8]

There is one problem with this, however. If nesting is a string or string-like object, it is a sequence and will not raise TypeError, yet you do not want to iterate over it.

 

¦¦Note There are two main reasons why you shouldn’t iterate over string-like objects in the flatten function. First, you want to treat string-like objects as atomic values, not as sequences that should be flattened.

 

Second, iterating over them would actually lead to infinite recursion because the first element of a string is another string of length one, and the first element of that string is the string itself!

 

To deal with this, you must add a test at the beginning of the generator. Trying to concatenate the object with a string and seeing if a TypeError result is a simplest and fastest way to check whether an object is string-like.1 Here is the generator with the added test:

def flatten(nested):

try:

Don't iterate over string-like objects: try: nested + ''

except TypeError: pass

else: raise TypeError for sublist in nested:

for element in flatten(sublist):

yield element

except TypeError:

yield nested

As you can see, if the expression nested + '' raises a TypeError, it is ignored; however, if the expression does not raise a TypeError, the else clause of the inner try statement raises a TypeError of its own. This causes the string-like object to be yielded as is (in the outer except clause). Got it?

 

Here is an example to demonstrate that this version works with strings as well:

list(flatten(['foo', ['bar', ['baz']]])) ['foo', 'bar', 'baz']

 

Note that there is no type checking going on here. I don’t test whether nested is a string, only whether it behaves like one (that is, it can be concatenated with a string).

 

A natural alternative to this test would be to use isinstance with some abstract superclass for strings and string-like objects, but unfortunately, there is no such standard class. And type checking against str would not work even for UserString.

Generators in General

Generator Methods

We may supply generators with values after they have started running, by using a communications channel between the generator and the “outside world,” with the following two endpoints:

 

The outside world has access to a method on the generator called send, which works just like next, except that it takes a single argument (the “message” to send—an arbitrary object).

 

Inside the suspended generator, yield may now be used as an expression, rather than a statement. In other words, when the generator is resumed, yield returns a value—the value sent from the outside through send. If next was used, yield returns None.

 

Note that using send (rather than next) makes sense only after the generator has been suspended (that is after it has hit the first yield). If you need to give some information to the generator before that, you can simply use the parameters of the generator-function.

Here’s a rather silly example that illustrates the mechanism:

def repeater(value):

while True:

new = (yield value)

if new is not None: value = new

Here’s an example of its use:

r = repeater(42)

next(r)

42

r.send("Hello, world!") "Hello, world!"

Note the use of parentheses around the yield expression. While not strictly necessary in some cases, it is probably better to be safe than sorry and simply always enclose yield expressions in parentheses if you are using the return value in some way.

 

Generators also have two other methods.

The throwing method (called with an exception type, an optional value, and traceback object) is used to raise an exception inside the generator (at the yield expression).

 

The close method (called with no arguments) is used to stop the generator.

 

The close method (which is also called by the Python garbage collector, when needed) is also based on exceptions. It raises the GeneratorExit exception at the yield point, so if you want to have some cleanup code in your generator, you can wrap your yield in a try/finally statement.

 

If you wish, you can also catch the GeneratorExit exception, but then you must reraise it (possibly after cleaning up a bit), raise another exception, or simply return. Trying to yield a value from a generator after close has been called on it will result in a RuntimeError.

 

Simulating Generators

If you need to use an older version of Python, generators aren’t available. What follows is a simple recipe for simulating them with normal functions.

 

Starting with the code for the generator, begin by inserting the following line at the beginning of the function body:

result = []

 

If the code already uses the name result, you should come up with another. (Using a more descriptive name may be a good idea anyway.) Then replace all lines of this form:

yield some_expression with this:

result.append(some_expression)

Finally, at the end of the function, add this line:

return result

Although this may not work with all generators, it works with most. (For example, it fails with infinite generators, which of course can’t stuff their values into a list.)

 

Here is the flatten generator rewritten as a plain function:

def flatten(nested):

result = []

try:

Don't iterate over string-like objects: try: nested + ''

except TypeError: pass

else: raise TypeError for sublist in nested:

for element in flatten(sublist):

result.append(element)

except TypeError:

result.append(nested)

return result

 

Recommend