Calling a class from a module

This is the place for queries that don't fit in any of the other categories.

Calling a class from a module

Postby eva12 » Fri May 23, 2014 7:15 am

Hello!

What is the format to call a class from a separate module?

For example if I wanted to call the function test() from module hello.py then I would write hello.test()
(please correct me if I am wrong)
Now I want to call a class called classone from hello.py
I tried hello.classone() with parameters and I get an error:
AttributeError: 'classone' object has no attribute 'hello'

Again, what is the correct format to call a class from another module?

Thank you :)
Last edited by Mekire on Fri May 23, 2014 8:50 am, edited 1 time in total.
Reason: First post lock.
eva12
 
Posts: 1
Joined: Fri May 23, 2014 7:09 am

Re: Calling a class from a module

Postby metulburr » Fri May 23, 2014 10:21 am

For example if I wanted to call the function test() from module hello.py then I would write hello.test()
(please correct me if I am wrong)
Now I want to call a class called classone from hello.py
I tried hello.classone() with parameters and I get an error:
AttributeError: 'classone' object has no attribute 'hello'

Again, what is the correct format to call a class from another module?


It all depends on how you import it

you can do
Code: Select all
from modulename import Klass
obj = Klass()

Code: Select all
import modulename
obj = modulename.Klass()


You can also check out this tutorial, which all 3 parts may go into more detail than you wish but may answer your question.

AttributeError: 'classone' object has no attribute 'hello'

Based on your post, this looks like you trying to call a global function hello() from an object derived from classone, in which classone does not have that method called hello()? You are going to have to show the exact code you have. Please read what to include in a post
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1312
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: micseydel, Mothosane and 5 guests