0

I have written a python script that defines a class and creates some objects. I want to be able to work with the objects from the python console, but I can't figure out how.

For example, if I had defined a class and created an object in a file called Elements.py:

class Element:

    def __init__(self, name, protons, mass):
        self.name = name
        self.protons = protons
        self.mass = mass

element_Al = Element('Aluminium', 13, 26.982)

I thought that from the python console I could do the following to access the object's information:

import Elements.py

print(element_Al.mass)

I assumed that this would print 26.982, however this doesn't work (even though I am in the same directory as the file). What am I doing wrong?

1
  • 1
    Explain "doesn't work". If there is an error message show it as properly formatted text in the question. Commented May 31, 2021 at 18:50

1 Answer 1

1

You can try:

from Elements import *
print(element_Al.mass)

or

import Elements
print(Elements.element_Al.mass)
Sign up to request clarification or add additional context in comments.

Comments

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.