1

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!

  • 3
    Never heard of range as an operator and I don't think is a keyword. Is true that it is a function and hence and object, because all functions are objects in Python – Dani Mesejo Oct 18 '21 at 16:46
  • 3
    It's not an operator or keyword. – Barmar Oct 18 '21 at 16:46
  • 1
    `range` is a class that implements the iterator protocol. – Barmar Oct 18 '21 at 16:47
  • In Python 2 `range` was a function that returned a list, but as someone new to Python, that's just a piece of historical trivia, not something you need to worry about unless you are porting or maintaining older code. – chepner Oct 18 '21 at 16:53
  • Something you might find helpful are the [docs for operators](https://docs.python.org/3/library/operator.html) and [the docs for kewords](https://docs.python.org/3/library/keyword.html). For example, the operator docs list all operators and define what they are. Keyword has functions like `import keyword; keyword.iskeyword(range)` or the attribute `keyword.kwlist` which can help you test for yourself – G. Anderson Oct 18 '21 at 16:53
  • 1
    `range` in Python 3.x isn't even a generator in the usual sense because it can be indexed and consumed repeatedly, which generators typically cannot be. – sj95126 Oct 18 '21 at 16:57
  • @sj95126 It's a "generator", just not a `generator`. `range` is an *iterable*, and implements `__iter__` by returning a *new* instance of `range_iterator` (that is, like all *iterators*, consumable only once) each time it is called. – chepner Oct 18 '21 at 17:04
  • Thank you so much to everyone for these helpful answers, and for being patient with a newbie! – Mark Featherstone Oct 18 '21 at 17:41
  • Could I then clarify the following: Would I be correct to say that the input below is a range function, while the output is a range instance of a Sequence data type? In: range(0,5,1) Out: 0 1 2 3 4 Thanks! – Mark Featherstone Oct 18 '21 at 18:47
  • `range(0,5,1)` is you _instantiating a range object_ , or creating a new object of the `range` type. – G. Anderson Oct 18 '21 at 19:13
  • Thanks very much! – Mark Featherstone Oct 18 '21 at 21:48

1 Answers1

2

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

G. Anderson
  • 5,815
  • 2
  • 14
  • 21
  • WOW!!! Thank you so much! (I do not yet have upvote privileges, or I would.) – Mark Featherstone Oct 20 '21 at 15:22
  • 1
    Cheers, thanks for asking the question to begin with, there were several things even I didn't understand about range that this taught me! If this helped you out, you can use the green check mark to accept the answer – G. Anderson Oct 20 '21 at 15:28
  • Done! Thanks again. – Mark Featherstone Oct 20 '21 at 15:38
  • And, not to overburden the point, [this question](https://stackoverflow.com/questions/44571718/python-3-range-vs-python-2-range) has some interesting discussion about the change from range being a function in python 2 to being it's own type in python 3 (probably a good portion of why it's still referred to as a function even though it no longer is) – G. Anderson Oct 20 '21 at 16:03
  • Oh, that's a really good point. – Mark Featherstone Oct 21 '21 at 17:34