2

In Think Python 2e "class" is defined as "A programmer-defined type. A class definition creates a new class object."

But isn't a built-in type considered a class too?

Using Python 3.4.0, the following code:

class File:
    """Represents a computer file."""
    ...

print(type(str))
print(type(File))
print(type("Mahmud"))
print(type(File()))

has the following output:

<class 'type'>
<class 'type'>
<class 'str'>
<class '__main__.File'>

. So here we can notice that the instances of both the built-in type str and the programmer-defined type File belong to <class 'str'> and <class '__main__.File'>, respectively, so both str and File are considered classes, as I understand. So, is the definition right? If so, how is it right and what do I misunderstand or don't know?

3

But isn't a built-in type considered a class too?

Yes, it is. The author of the article you linked to is somewhat correct, but his definition is lacking. A more complete definition of a class in Python is: any type that is defined whether user-created or built-in. A class is essentially a blueprint for creating an object, regardless of whether the "blueprint" is defined at the C level - such as with builtins - or at the Python level such as with user defined types.

However, normally when you think of a class in Python, you are thinking specifically about user-defined classes. But as I made clear above, that's not necessarily the correct way to think of classes. So while the definition may not be completely wrong, it is not correct either.

  • Thank you. What do you mean by "technically correct"? How can he be so, when his definition is lacking? – Mahmud Muhammad Naguib Jul 6 '17 at 21:01
  • 1
    @MahmudMuhammadNaguib What I meant to convey is what amon said as well. The author's definition of a class is correct, but would probably mislead one to think that is exclusive what classes are. – Christian Dean Jul 6 '17 at 21:08
  • 1
    @MahmudMuhammadNaguib But I in hindsight, I think "technically" is the wrong word, as the docs do mention list, int and the other builtin types as classes. "somewhat" would probably fit better. – Christian Dean Jul 6 '17 at 21:14
4

In Python 2, you'll notice that built-in types are sometimes distinct from user-defined types (classes):

>>> type(str)
<type 'type'>
>>> type("Mahmud")
<type 'str'>
>>> str
<type 'str'>

>>> class OldStyleClass:
...    pass
...
>>> type(OldStyleClass)
<type 'classobj'>
>>> type(OldStyleClass())
<type 'instance'>
>>> OldStyleClass
<class ...OldStyleClass at 0x...>

>>> class NewStyleClass(object):
...    pass
...
>>> type(NewStyleClass)
<type 'type'>
>>> type(NewStyleClass())
<class 'NewStyleClass'>
>>> NewStyleClass
<class 'NewStyleClass'>

So we have three distinct cases:

  • builtin types like str
  • old style classes
  • new style classes which inherit from object.

           type(X)            X                   type(X())
           -----------------  ------------------  -----------------
builtin    <type 'type'>      <type 'X'>          <type 'X'>
old style  <type 'classobj'>  <class X at 0x...>  <type 'instance'>
new style  <type 'type'>      <class 'X'>         <class 'X'>

The differences between these cases are subtle and have become less important over time. In earlier Python versions, the internal representation affected the reflection capabilities. A remnant of these issues in Python 2.7 is that type(X()) == X is false when X is an old-style class!

So for Python 2.x there are some differences between builtin-types and user-defined classes, even though all of them can be used as classes (instantiated, inherited from, …). I consider the author's statement correct, though slightly misleading. It would be more correct to say:

The class keyword allows programmers to define additional classes.

or

Each value has a class or type which contains available methods. You can add your own classes with the class keyword.

(Of course, a class definition is not the only way to define a new class. The type(…) constructor comes to mind, as would C extensions.)

In Python 3, these three cases have been combined and there is no visible difference between built-in and user-defined classes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.