Type of user defined class objects - Python

I am not very experienced with classes or OOP, and I'm confused with some of the results I'm getting.

I am trying to use type() on a class I have created.

    class TestClass:
        pass

    my_class = TestClass()
    print type(my_class)

The above code returns

    <type: 'instance'>

Based on my understanding my_class should be initialized as a TestClass object.. right? Why isn't the output TestClass. Also, if possible, what would I need to change to get this result?

Answers


From the documentation:

Classes and instances come in two flavors: old-style (or classic) and new-style.

Up to Python 2.1, old-style classes were the only flavour available to the user. The concept of (old-style) class is unrelated to the concept of type: if x is an instance of an old-style class, then x.__class__ designates the class of x, but type(x) is always <type 'instance'>. This reflects the fact that all old-style instances, independently of their class, are implemented with a single built-in type, called instance.

By not inheriting from object, you create an old-style class, so type(my_class) behaves like it does.

Inherit from object and your problems will go away:

class TestClass(object):
    pass

In Python 3, all classes are new-style classes, which is nice.


If you want TestClass to be an object, you have to specify it like so

class TestClass(object):
    pass

Need Your Help

How can I change this code from an applet to a standalone application

java applet

Does anyone know how to make this applet into an application? After I remove "extends applet", It doesn't work. I'm not sure how to change it into an application and what to write after "extends."

Twitter auth with devise and twitter api

ruby-on-rails twitter devise omniauth

I manually requesting a request token from twitter and pass the callback to the default page for a device, but get an error