[Tutor] Follow Up

Kent Johnson kent37 at tds.net
Mon Feb 19 02:28:45 CET 2007


kubota2550 at gmail.com wrote:
> I thought that I would be able to treat this like one of the standard
> 
libraries (ie random) where I just include the random lib and then use a
command such as random.randrange(x,y). Will this not work with a custom
class?

Yes you can do that with a custom class or with a plain function. With a 
class you have to import and instantiate the class as Rikard showed.

Kent

PS Please respond to the list, not to me personally.

> 
> 
> Rikard Bosnjakovic wrote:
>>/ On 2/18/07, kubota2550 at gmail.com <http://mail.python.org/mailman/listinfo/tutor> <kubota2550 at gmail.com
>  <http://mail.python.org/mailman/listinfo/tutor>> wrote:
> />/ 
> />>/ import classctof
> />>/ y=classctof.fahrenheit(1)
> />>/ print y
> />>/
> />>/ What am I doing wrong to get it to pass the answer back to the calling
> 
> />>/ program?
> />/ 
> />/ You need to instancify the class first before calling its methods:
> />/ 
> />/ 
> />/ import classctof
> />/ 
> />/
>  # make an instance
> />/ y = classctof.Temperature()
> />/ 
> />/ # add the property. it's usually better to let the method handle this
> />/ y.celcius = 42
> /
> The __init__() method, in particular:
> 
> class Temperature(object):
>    def __init__(self, celcius):
>      self.celcius = celcius
> etc.
> 
> But in this example there doesn't seem to be any need fo a class to hold 
> this function. You could make a standalone function 
> 
> celciusToFahrenheit() that takes the celcius temperature as a parameter 
> and returns the fahrenheit temperature.
> 
> Kent
> 




More information about the Tutor mailing list