Python Forum
trouble importing class definition from one .py into another
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
trouble importing class definition from one .py into another
#1
Problem: I'm having issues calling classes in a program.

I created a program named example.py which has the following code:
class ExampleBase:

    """
    This is the ExampleBase class
    """

    def __init__(self, company_name="N/A", stock_dict={}):
        """
        class constructor
        """
        self.company_name = company_name
        self.stock_dict = stock_dict     
        return
    
    def __str__(self):
        """
        Prints the company name string
        """        
        str = "The Company name is: %s" %\
            (self.company_name
            )
        
        return str
    
    
    def add_purchase(self, addtlSTK):
        """
        Adds item to stock_dict
        """
        self.stock_dict.update(addtlSTK)
        return
I'm attempting to call ExampleBase in another program whose code is:
import example

if __name__ == "__main__":
    a = {"10-01-2014":(10, 11.25), "10-02-2014":(11, 12.25), "10-03-2014":(12, 13.25)}
    b = example.ExampleBase("Bern", a)
I'm getting this error: AttributeError: 'ExampleBase' object has no attribute 'Examplebase'
Reply
#2
It runs for me. is the name of the ExampleBase script example.py?
Reply
#3
It started working for me as well.
Not sure why, I didn't change anything Think
Reply
#4
You probably changed what you typed!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Return a definition samh625 6 2,367 Jul-28-2020, 08:32 AM
Last Post: samh625
  New to coding. An error occurs with no definition westernwhiskey 4 2,936 Mar-15-2018, 12:37 PM
Last Post: westernwhiskey
  List not passing between methods inside a class definition ijosefson 5 4,379 Oct-15-2017, 07:15 PM
Last Post: ijosefson

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020