Objects, Classes, and Instances in Python

Posted on 2006-04-27
Last Modified: 2008-03-17
I've written up this class:

class sphere:

    def __init__(self, radius):
        surfaceArea = 4.0 *  pi  * radius ** 2
        volume = 4.0 / 3 *  pi  * radius ** 2

    def getRadius(self):
        return self.radius

    def surfaceArea(self):
        return self.surfaceArea

    def volume(self):
        return self.volume

def main():
    radius = input("Enter the radius of the sphere: ")
    sphere = sphere(getRadius, surfaceArea, volume)
    print sphere.getRadius
    print sphere.surfaceArea
    print sphere.volume


When I run it it says "UnboundLocalError: local variable 'sphere' referenced before assignment"

What have I done wrong?
Question by:blackberrycobbler
    LVL 17

    Expert Comment

    There are numerous problems.

    1 - sphere is a class definition. When you write sphere = sphere(...) you are asking Python to overwrite the class definition with an instance of the class.
    so - use a name other than the class name on the left of the assignment. It is a convention to capitalize class names. So you could name the class Sphere then write sphere = Sphere(...)

    2 - the __init__ method expects 1 parameter (radius). You are passing 3. Why? And the names you are passing are undefined globals. And the __init__method does not save radius.

    3 - pi is undefined.

    4 - in the __init__ method you are creating some local variables (surfaceArea and volume) If you want them to be class members you should write self.surfaceArea just as in the surfaceArea method.

    5 - You have another name conflict: surfaceArea originally defined as a function will be overwritten by a number.

    6 - print sphere.getRadius will give you something like:
    <bound method sphere.getRadius of <__main__.sphere instance at 0x00CC7EB8>>

    To call a method add() at the end.

    Fix that stuff and come back with a new program.

    Author Comment

    Thanks, ramrom, for the prompt reply.  I am working on it my revision and  I will be back with the results.  
    LVL 17

    Accepted Solution

    Thanks for your reply. I'm glad it might help.

    I pondered  sphere = sphere(...). This (by itself) is acceptable, you just no longer have direct access to the class definition.

    Please do not hesitiate to ask questions. We are here to help and sometimes we offer something that does not meet your need.
    LVL 17

    Expert Comment

    You accepted an answer, and I'm curious to know how it helped. Do you have a working version of the program to show us?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
    When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
    Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
    Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now