0

testa.py

class A:

    s1 = 333
    __age = 0

    def __init__(self,age ):
        self.__age=age
        return

    def __del__(self):  

        return  
    #private
    def __doSomething(self, s): 
        print self.__age 
        return            
    #public
    def doSomething(self, s):  
        self.__doSomething(s)    
        print s

test.py

import sys
import testa

a=A(111)
a.doSomething('222')

run

python test.py

it reports error:

NameError: name 'A' is not defined

your comment welcome

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
arachide
  • 8,006
  • 18
  • 71
  • 134

2 Answers2

3

Use
a=testa.A(111)

You must name the package unless you import A explicitly e.g

from testa import A

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
0

Remember this:

Doing: import mymodule does not import the whole methods and attributes of mymodule to the namespace, so you will need to refer to mymodule, everytime you need a method or attribute from it, using the . notation, example:

x = mymodule.mymethod()

However, if you use:

from mymodule import * 

This will bring every method and attribute of mymodule into the namespace and they are available directly, so you don't need to refer to mymodule each time you need to call one of its method or attribute, example:

from mymodule import *

x = mymethod()  #mymethod being a method from mymodule

You can also import specific method if you don't want to bring the whole module:

from mymodule import myMethod

For further details, read the Python docs:

https://docs.python.org/2/tutorial/modules.html

Iron Fist
  • 10,739
  • 2
  • 18
  • 34