[Tutor] Learning class code and function

Albert-Jan Roskam fomcl at yahoo.com
Tue Jan 20 21:23:30 CET 2015


-----------------------------
On Tue, Jan 20, 2015 4:44 PM CET Alan Gauld wrote:

>On 20/01/15 12:04, jarod_v6 at libero.it wrote:
>> Dear all, I continue to try to understand some code:
>> class Tutto():

I think you need a new-style class wit property.

"Return a property attribute for new-style classes (classes that derive from object)."
https://docs.python.org/2/library/functions.html#property

I recently discovered that properties are implemented with descriptors: 
https://docs.python.org/2/howto/descriptor.html

>>      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):

Might be useful to insert the following here (?)
if hasattr(self, "_readsets"):
    return self._readsets

>      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?

"tutto" means "everything"? Maybe FortyTwo would be a cool name. :-)



>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
>
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>To unsubscribe or change subscription options:
>https://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list