Home > Software engineering >  Need guidance in understanding how range or range() can be described in so many ways
Need guidance in understanding how range or range() can be described in so many ways

Time:10-21

I am very, very new to programming and am starting with Python. I am about thirty videos into a popular online course. In the most recent tutorial, the instructor introduces range [for example, range(0,10,1)]. In a just a few minutes, the tutor refers to range or range() as:

  • an operator
  • a function
  • a keyword
  • a generator

Range is also, as it turns out, within the Sequence data type, and I suppose it can also be called an 'object', right? I understand that the same "thing" can be described in different ways depending on the context. For example, it's no contradiction to say that I am both a Canadian and a husband. The problem is to keep all these categories straight in my head when trying to understand the role or state of a given term in Python. Is there some rubric or chart or method or set of definitions to understanding all the roles that a given "thing" plays in Python? Thanks for your help!

CodePudding user response:

Rather than just answering what range() is, below is an attempt at showing how to investigate what something is or isn't in python. We can check two things at each step, the abstract range, and your initial example assigned to a variable (q is a terrible variable name but useful for brevity):

q=range(0,10,1)

We can also import some help with checking for types and keyword:

import types
import keyword

Now, let's look at your proposed options for "what is it"?

an operator

In python, built-in operators "perform object comparisons, logical operations, mathematical operations and sequence operations" (From the operator module docs ) You can see a long list of operators in the link provided, and range() isn't there.

a function (or method)

This is one of the more confusing ones at first glance, since googling "python range function" gives thousands of very authoritative hits. Also, range is even listed in the documentation for built-in functions. That said, it's not a function according to those same docs: "class range(start, stop [,step])". We can test this as below, using one of our earlier imports and the pythonic method for checking whether something is what you think it is, isinstance():

isinstance(range, types.FunctionType)
False
isinstance(q, types.FunctionType)
False

isinstance(range, types.BuiltinFunctionType)
False
isinstance(q, types.BuiltinFunctionType)
False

isinstance(range, types.BuiltinMethodType)
False
isinstance(q, types.BuiltinMethodType)
False

a keyword

Thankfully, this is much more straightforward.

import keyword
keyword.iskeyword(range)
False

You can also use print(keyword.kwlist) to see a list of all the keywords in python

a generator

As mentioned in the comments, while range() does generate something, it isn't a generator type object in python. Let's double check:

isinstance(range, types.GeneratorType)
False
isinstance(q, types.GeneratorType)
False

Now we can look at some other options, starting with the docs for range to get an authoritative answer: "The range type represents an immutable sequence of numbers..."

Understandably, this is accurate but hard to parse for someone first learning. The simplest way to check what type an object is (besides using types and isinstance) is to just check the type(). Which gives the charming tautological definition, for example:

type(type)
type

So what is range, and what is an instance of a range?

type(range)
type

type(q)
range

And, to finish off the last part of your question, is it an object? Everything in python is an object.

isinstance(q, object)
True

So, from the above, we can deduce that the answer to "what is a range in python?" is: Range is a type, and a defined range is an instance of an object of the range type

  • Related