Classes in python

4

I know that this is basic, but we could say that I am a bit new in python and there are things that I do not know. I want to make a call for a function within another function, they are all within the same class. Example:

Import ClassHeredada

Class MyClass(ClassHeredada):
    def mi_funcion1(nombre):
        return nombre
    def mi_retorno(nombre):
        print mi_funcion1(nombre)

Show me this error:

NameError: global name 'mi_funcion1' is not defined

I do not realize where the error is.

    
asked by Nahuel Jakobson 23.11.2016 в 00:43
source

3 answers

3

I do not know exactly what you intend to do, but should not the two functions be class methods? I mean, something like this:

class MyClass():
    def mi_funcion1(self, nombre):
        return nombre
    def mi_retorno(self, nombre):
        print self.mi_funcion1(nombre)

miObjeto = MyClass() #instancia de Myclass
miObjeto.mi_retorno('Pepito')

Note the use of the self parameter in each method of the class. It is a reference to the object whose method is called. The name is conventional, it can be anything but it is usually used self by convention, in this way everyone knows what it refers to. Internally it is a pointer to itself (hence the 'self').

    
answered by 23.11.2016 в 01:14
1

Thank you all for your answers, after so many hours of php code and then python, I was not realizing that I was not doing referencia at atributos of class MyClass . It's like @FJSevilla says:

class MyClass():
    def mi_funcion1(self, nombre):
        return nombre
    def mi_retorno(self, nombre):
        print self.mi_funcion1(nombre)

miObjeto = MyClass() #instancia de Myclass
miObjeto.mi_retorno('Pepito')

Here was my error: print self.mi_funcion1(nombre)

    
answered by 23.11.2016 в 01:26
0

Will not you lack a return ?

Import ClassHeredada

Class MyClass(ClassHeredada):
    def mi_funcion1(nombre):
        return nombre
    def mi_retorno(dato):
        print mi_funcion1(dato)

from console you have to import all the clase like this:

from MyClass import *
    
answered by 23.11.2016 в 00:59