Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instancing a class - difference between with and without brackets

Say I have a very simple class like the following:

class myClass:
    def __init__(self): 
        self.myProp = 2

If I instantiate using the brackets, everything works as I expect:

>>> a = myClass()
>>> a.myProp
2

However, if I don't use the brackets on the two lines above, i.e.:

>>> a = myClass

I get the following error:

>>> a.myProp
Traceback (most recent call last):
File "<pyshell#45>", line 1, in <module>
a.myProp
AttributeError: class myClass has no attribute 'myProp'

If I print the object,

>>> a = myClass
>>> a

I get

<class __main__.myClass at 0x0275C538>

It seems that a is an instance of the class, but somehow is not initialized. In other languages, I would expect a compile error if trying to cast a class instance into an object without initalizing it (e.g. in C#, myClass a = new myClass(); would work fine but myClass a = new myClass; would return a compile error).

So my question is: what is, technically speaking, the object a = myClass without brackets?

like image 997
Matteo NNZ Avatar asked Feb 03 '15 22:02

Matteo NNZ


People also ask

Does class need brackets Python?

In Python, the preferred syntax for a class declaration without any base classes is simply: class A: ... Don't use parentheses unless you are subclassing other classes. The docs on the matter should give you a better understanding of how to declare and use classes in Python.

What is the difference between a class and an instance of a class Python?

Class variables can only be assigned when a class has been defined. Instance variables, on the other hand, can be assigned or changed at any time. Both class variables and instance variables store a value in a program, just like any other Python variable.

How do I know if a class is instance of a variable?

Using isinstance() function, we can test whether an object/variable is an instance of the specified type or class such as int or list. In the case of inheritance, we can checks if the specified class is the parent class of an object. For example, isinstance(x, int) to check if x is an instance of a class int .

What is the difference between a class and an object or instance in Python?

Everything in Python is an object such as integers, lists, dictionaries, functions and so on. Every object has a type and the object types are created using classes. Instance is an object that belongs to a class. For instance, list is a class in Python.


1 Answers

a is the class itself -- In python, classes are first class objects1. You can pass them around as parameters, alias them to different names (as you've done in your example) and then you can instances from any reference that you have in the current namespace.

a = myClass  # a and myClass identical at this point.  The interpretter won't care which you use.
a_instance = a()  # instance of myClass

def make_instance(cls):
    return cls()

another_instance = make_instance(a)
yet_another_instance = make_instance(myClass)

You see, python doesn't have any "compile time checking" because really -- there is no compile time. Python code gets interpreted at runtime. True, you can have SyntaxErrors pop up at when you import something, but that is still during runtime.

1No pun intended

like image 130
mgilson Avatar answered Oct 12 '22 08:10

mgilson