[Tutor] class and methods/functions

Eric Walker ewalker at micron.com
Thu Oct 6 20:21:43 CEST 2005


Sorry Kent,
In my haste to just simulate I did make the example with syntax errors. Let me 
try this again and I guess your teaching me how to use this mailing list  the 
correct way also.  Ok this is the actual code. I am able to get it to run 
from the command line.

Thanks kent for your patience with this Python Newbie....



class TPROJ:    
    import re
    import string
    def display(self):#display method
        print self.BASENAME
        print self.PROJECT
        print self.REV
        print self.DESIGNATOR
        print self.TYPE
        
    def __init__(self,value):#createMethod auto executes since it has __
        name = nameCheck(value)
        if name:
            self.BASENAME = value
            self.PROJECT = value.split(':')[0] 
            self.REV = value.split(':')[1]
            self.DESIGNATOR = "NOVALUE"
            self.TYPE = ('SW','TMOD','SWA','TMODA')#constant tuple
        if not name:
            print "found a bad name: %s" % value
            
            
    
def nameCheck(value):#checks to see if filename is valid. Has a colon for 
split
        import re 
        tempREG = re.match('.*:.*',value)
        return str(tempREG) != 'None'
            
def getProjectNames():
    import os
    currentDir=os.getcwd()
    nameTable = {}
    temp=currentDir + '/TEMP'
    print temp
    os.chdir(temp)
    baseList=os.listdir(".")
    baseObjectList = []
    for name in baseList:
        baseObjectList.append(TPROJ(name))
        
    os.chdir(currentDir)    
    for item in baseObjectList:
        print item.BASENAME
        
        
try:
    getProjectNames()
except:
    print "Got an Error"


On Thursday 06 October 2005 12:05 pm, Kent Johnson wrote:
> 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.
>
> This is full of syntax errors...
>
> > class yes:
> >  def func1
>
> should be
>
>   def func1(self):
> >   temp = re.match #####
>
> OK but not used for anything
>
> >   return str(tempREG != 'None'
>
> Missing close paren and tempREG is not defined so you will get a NameError
> at runtime
>
> >         def display(self):
>
> This should have the same indentation as def func1; indentation is
> significant!
>
> >   print all the class attributes....
> >  def __init__(self,value):
> >   name = func1(value)
>
> probably you want self.func1(value) - to call a member function from inside
> another member function you have to prefix the name with self.
>
> >   other stuff
> >
> > def func2():
> >  a = yes()
> >
> > try:
> >  func2()
> > except:
> >  print "error"
>
> Generic except: blocks like this are a bad idea, it hides useful
> information without providing any benefit. The traceback that Python prints
> on an uncaught exception may look like a lot of gibberish at first but it
> contains a wealth of useful information that is thrown away by this
> handler.
>
> HTH,
> Kent
>
> > 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
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
Eric Walker
EDA/CAD Engineer
Work: 208-368-2573


More information about the Tutor mailing list