2

I am new to Python and wonder if someone can help me with this simple query.

In Visual Studio Code I have created a class in a .py file. I have then run the file.

My question is how do I actually instantiate an object? I have tried unsuccessfully to do this in the Terminal window.

Thanks

Clos

1
  • 2
    just like this: alex = Student(). You should read the official python documentation. here Commented May 1, 2020 at 12:42

1 Answer 1

1

Assume that you have created a script called test.py. And in this script you've created a class called Example like so:

Class Example:
    def __init__(self):
        print("Example() instance is created!!")

Now, you can instantiate this class using:

x = Example()

But if you want to use this class in the terminal, then you need first to import the class like so:

$ python #to enable python shell in terminal
>>> from test import Example
>>> x = Example()
Example() instance is created!!
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer. My actual issue is indeed how to use the class in the Python terminal in Visual Studio Code. Unfortunately if I follow your example I receive the message The 'from' keyword is not supported in this version of the language. At line:1 char:30
In terminal, you need to activate python shell first before using my example. You can do that by running python in the terminal. Anyway, I've edited my answer to include this part.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.