Rakibul Islam
Rakibul Islam

Reputation: 39

What is classobject in python is it same as class name?

class Test:
    def __init__(self):
        print('Object reference:', id(self))
        print('Class object reference', id(Test))


t = Test()

Object reference: 2170838573008

Class object reference: 2170806511808

Upvotes: 0

Views: 93

Answers (3)

S.B
S.B

Reputation: 16496

when you use class keyword, you are actually creating an instance of type type. Classes are instances themselves.

class Test:
    pass

print(isinstance(Test, type))  # True
print(type(Test))              # <class 'type'>

here, Test is just a label in your global namespace, which points to this instance you have created.

Now when you call your class, here Test, you are creating an instance of it. self inside your class, points to this object (instances of your class).

Upvotes: 0

Vishal Kamlapure
Vishal Kamlapure

Reputation: 590

Class Name is not same as class object. When you create an instance of class that time you create an object for that class. In your case t is a Object of class Test.

Almost everything is object in python. So as your class is a type of object.

class Test:
    def __init__(self):
       print('Object reference:',id(self))
       print('Class object reference',id(Test))


t = Test()  // Here t is object of class Test.

Upvotes: 1

TheEagle
TheEagle

Reputation: 5992

It is not the same as class name. Everything is an object in Python. Classes are, and their instances, too. Even modules, and functions, just everything.

Upvotes: 1

Related Questions