4

I'm learning the basics of python on classes and objects.

I have created a basic class object with getters, setters and __str__ function.

'''
Created on 02/06/2012
@author: rafael
'''
class Alumno(object):
 '''
 Esta clase representa a un alumno de la ibero
 '''
 __nombre=None
 __idAlumno=None
 __semestre=0
 def __init__(self,nombre,idAlumno,semestre):
 '''
 Constructor
 '''
 self.__nombre=nombre
 self.__idAlumno=idAlumno
 self.__semestre=semestre
 def Alumno(self):
 return self
 def getId(self):
 return self.__idAlumno
 def setId(self,idAlumno):
 self.__idAlumno=idAlumno
 def getNombre(self):
 return self.__nombre
 def setNombre(self,nombre):
 self.__nombre=nombre
 def getSemestre(self):
 return self.__semestre
 def setSemestre(self,semestre):
 self.__semestre=semestre
 def __str__(self):
 info= "Alumno: "+self.getNombre()+" - id: "+self.getId()+" - Semestre:"+str(self.getSemestre())
 return info

And a python module that imports that class and initialize the object for print their info.

'''
Created on 02/06/2012
@author: rafael
'''
from classes import *
if __name__ == '__main__':
 alumno=Alumno("Juanito Perez","1234",2)
 print alumno

But I'm having a NameError Exception, so I must create my object in this way:

alumno=Alumno.Alumno(param,param,param)

But I want to be on this way:

alumno=Alumno(param,param,param)

Can someone explain to me how classes work or what I'm doing wrong?

Chris Morgan
91.6k28 gold badges217 silver badges220 bronze badges
asked Jun 2, 2012 at 8:03
4
  • setters and getters are so superfluous in Python. Forget your Java background. Python is not Java. Assign your instancen variable once inside the constructor and use them as properties. setter and getter methods in this context are completely not necessary. If you really need them then use the property() method or @property decorator. Commented Jun 2, 2012 at 8:54
  • 2
    Why are you using getter/setters? It's not pythonic at all! Use member variables and if you ever need getter/setter logic create a property, i.e. a member variable that will call a getter/setter function automatically when accessed. Commented Jun 2, 2012 at 8:56
  • def Alumno(self): return self!? You really need to learn the Python way. Python ain't Java. Commented Jun 2, 2012 at 8:59
  • 1
    hmm i don't know what you understand by "i'm learning" ... i just trying to learn a new language and that isn't reason for vote down .. Commented Jun 2, 2012 at 17:42

1 Answer 1

5

Ah, welcome to the issue of python namespace verbosity.

Unlike Java, where if you have a class named Foo in a file Foo.java, your class is not automatically hoisted into the module namespace.

If you want this behavior, you will need to do:

from Alumno import Alumno

There are other difficult ways around this. If for example you have a directory:

package/
 ...
 submodule/
 YourClass.py
 YourClass2.py
 __init__.py

You can do from YourClass import YourClass in __init__.py. Thus outside of the submodule, you can do import submodule.YourClass or from submodule import YourClass and get your desired behavior.

answered Jun 2, 2012 at 8:12
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your answer!! :D ... hmm if i have 2 or many modules on a package? like classes.Class1 and classes.Class2 ... i must to do a import for each one?
... but don't ever do it that way. That's not how you should do things in Python.
@rafuru -- if you have Class1 and Class2 in Alumno.py, you can do "from Alumno import Class1, Class2". Or you can just "import Alumno" and use "Alumno.Class1" and "Alumno.Class2"
@rafuru: in that case, the second method is probably what you want: you would import Class1 and Class2 into the __init__.py of submodule. Then you could do from submodule import *. It's frowned upon to do this unless you plan to use all or most of the classes you define in the file you're importing to (though I often do it for math and itertools which ought to be in the main namespace). The alternative would be from submodule import Class1,Class2 or just import submodule and refer to it as submodule.Class1 as Chris Morgan said.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.