[Tutor] Class and methods?

Kevin python.programming at gmail.com
Thu Mar 31 00:44:24 CEST 2005


I am sorta starting to get it. So you could use __init__ to ask for a
file name to see if there is one in a folder or not if there is then
open that file and conitue where that file left off. If its not there
create a new file with that name, then start the program? Or do I have
that all wrong?

Thanks
Kevin

On Wed, 30 Mar 2005 21:46:36 +0100, Alan Gauld <alan.gauld at freenet.co.uk> wrote:
> 
> > In a class is every def called a method
> 
> Strictly speaking only those that have a 'self' parameter(or
> equivalent)
> The others are "unbound functions" and pretty useless, usually being
> the result of programmer errors!...
> 
> > and the def __init__(self) is called the constructor method?
> 
> Usually.
> 
> > I don't have a problem writting a def I am still not clear on how to
> > use a constructor.
> 
> Use the constructor to initialise the attributes of your new instance.
> When you create an instance by calling a class:
> 
> instance = Class(a,b,c)
> 
> what happens is that Python creates a blank instance of Class
> then calls the __init__ method of the new instance passing in
> the arguments you gave to the class, in our case a,b,c.
> 
> [ If your class inherits from a superclass then its usually a
> good idea to call the superclass init method inside your init
> just to make sure the superclass attributes are all set up
> properly too. ]
> 
> > Is there a site that explains the constructor in
> > great detail?
> 
> Well I try... But it depends on how great a detail you want. "Great
> detail"
> implies "not easy to understand", in which case the definitive site is
> the
> Python documentation!
> 
> Alan G
> Author of the Learn to Program web tutor
> http://www.freenetpages.co.uk/hp/alan.gauld
> 
>


More information about the Tutor mailing list