Python Tutorial: Iterators and Scope- What Are They and How Do They Work?

///Python Tutorial: Iterators and Scope- What Are They and How Do They Work?

Python Tutorial: Iterators and Scope- What Are They and How Do They Work?

FavoriteLoadingAdd to favorites

Python iterator and scope

An iterator is an object that contains a countable number of values.
which can be iterated upon, meaning that you can traverse through all the values.

Lists, tuples, dictionaries, and sets are all iterable objects.
All these objects have a iter() method which is used to get an iterator:

To create an object/class as an iterator you have to implement the methods
__iter__() and __next__() to your object.
As you have learned in the Python Classes/Objects chapter, all classes have a function called __init__(), which allows you to do some initializing when the
object is being created.

The __iter__() method acts similar, you can do operations (initializing etc.),
but must always return the iterator object itself.

The __next__() method also allows you to do operations, and must return
the next item in the sequence.

Scope
A variable is only available from inside the region it is created. This is called scope.
A variable created inside a function belongs to the local scope of that function, and
can only be used inside that function.

source

By |2020-06-25T07:59:59+00:00June 25th, 2020|Python Video Tutorials|5 Comments

5 Comments

  1. Sarokar NGO June 25, 2020 at 8:00 am - Reply

    good video

  2. Ajm Chetry June 25, 2020 at 8:00 am - Reply

    Great job 👍

  3. ade caniago channel June 25, 2020 at 8:00 am - Reply

    💪💪

  4. Pankaj Prabha June 25, 2020 at 8:00 am - Reply

    Good

  5. Don Vega June 25, 2020 at 8:00 am - Reply

    Good

Leave A Comment

*