[Tutor] Learning class code and function
Alan Gauld
alan.gauld at btinternet.com
Tue Jan 20 16:44:20 CET 2015
On 20/01/15 12:04, jarod_v6 at libero.it wrote:
> Dear all, I continue to try to understand some code:
> class Tutto():
>
> def __init__(self):
> print "Ok"
> #@property
> def readsets(self,nome):
> self.nome = nome
> self._readsets = parse_tutto_readset_file(self.nome)
> return self._readsets
>
> Why If uncomment the decorator the code not work:
My understanding of the @property decorator is that it is only for
defining read-only properties. In other wordss it expects you to use it
like:
t = Tutto()
myvar = t.readsets
notice no parens and no parameters.
If you want to read the data from a file I'd suggest making the file
part of the constructor and then use readsets as shown above:
class Tutto:
def __init__(self, filename):
self.filename = filename
print 'ok'
@property
def readsets(self):
self._readsets = parse_tutto_readset_file(self.nome)
return self._readsets
p = Tutto("/home/mauro/Desktop/readset.csv")
var = p.readsets
I'd also question why you seem to have a function that has the class
name in its name? Should that function not be a method of the class?
Or is it part of some third party library perhaps?
hth
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list