[Tutor] class and methods/functions

Eric Walker ewalker at micron.com
Thu Oct 6 20:05:27 CEST 2005


Its working now. the moving of the function outside of the class allows me to 
run it from the command line.

Thanks

On Thursday 06 October 2005 11:56 am, Eric Walker wrote:
> Kent,
> I made  a correction the func1 I moved out of the class definition since it
> wasn't really a method. so it should look like this.
>
> > class yes:
> >      def display(self):
> >     print all the class attributes....
> >   def __init__(self,value):
> >     name = func1(value)
> >     other stuff
> >
> >  def func1
> >    temp = re.match #####
> >    return str(tempREG != 'None'
> >
> > def func2():
> >  a = yes()
> >
> > try:
> >  func2()
> > except:
> >  print "error"
>
> On Thursday 06 October 2005 11:47 am, Eric Walker wrote:
> > Kent,
> > Where I think my problem maybe in how I am running this. I want to
> > eventually run from the command line. I started python idle from my linux
> > command line and I was cut and pasting from my text file and seeing
> > things work. Now i want to run it from the command line and its
> > complaining. in my file I have something of the following.
> >
> > class yes:
> >  def func1
> >   temp = re.match #####
> >   return str(tempREG != 'None'
> >         def display(self):
> >   print all the class attributes....
> >  def __init__(self,value):
> >   name = func1(value)
> >   other stuff
> >
> > def func2():
> >  a = yes()
> >
> > try:
> >  func2()
> > except:
> >  print "error"
> >
> > On Thursday 06 October 2005 11:33 am, Kent Johnson wrote:
> > > Eric Walker wrote:
> > > > I have a class I am defining and then I call a function within that
> > > > class. Getting error that function call is not defined. Does the
> > > > function have to be created anywhere within a class or does it have
> > > > to be defined before the call within the class.
> > >
> > > Actual code and the error message (including the traceback) would be
> > > helpful here.
> > >
> > > Generally functions (usually called methods in this context) are
> > > defined within the body of a class and then called from outside the
> > > class. Functions have to be defined before they are  called but not
> > > before they are referenced.
> > >
> > > Very simple example:
> > >  >>> class B:
> > >
> > >  ...   def foo(self):
> > >  ...     print 'foo'
> > >  ...     self.bar()
> > >  ...   def bar(self):
> > >  ...     print 'bar'
> > >  ...
> > >
> > >  >>> b=B()
> > >  >>> b.foo()
> > >
> > > foo
> > > bar
> > >
> > > Kent
> > >
> > > _______________________________________________
> > > Tutor maillist  -  Tutor at python.org
> > > http://mail.python.org/mailman/listinfo/tutor
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list