Re: [Tutor] Class learning
On 23/01/15 01:44, jarod...@libero.it wrote: How can gave the attributes __name__ to a function? You don't Python does it for you. class Foo(object): def __init__(self): steps = {} tmp = open("rnaseq.base.ini","rb") config.readfp(tmp) readsets = parse_illumina_readset_file("/home/mauro/Desktop/readset.csv") You realise that steps is a local variable that is not used and gets thrown away. So its a waste of space. Similarly you read the config file but throw away the results. Again a waste of space. And the same with readsets. Your init does a lot of work to no long term effect. @property def steps(self): return [ self.one, self.two, self.fmit, ] def one(self): a = 5 return a ... #@property def show(self): ftp="\n".join([str(idx + 1) + "- " + step.__name__ for idx, step in enumerate(self.steps)]) print ftp It is working In [5]: F = Foo() In [6]: F.show() 1- one 2- two 3- fmit Yes, as expected. Why if I define the data in the same way I have this error? in () > 1 rna.show() in show(self) 261 #@property 262 def show(self): --> 263 ftp="\n".join([str(idx + 1) + "- " + step.__name__ for idx, step in enumerate(self.steps)]) 264 265 print ftp AttributeError: 'str' object has no attribute '__name__' Because you didn't define it in the same way. Consider this example from the pastebin: @property def star(self): print "Mitico Star" return "name" Here you make star a property so when in steps you store self.star you are not storing a reference to the method, as you did above, you are storing the return value of star - "name". Now in show() you try to take the __name__ of "name" but, as the error says, strings don't have __name__ attributes. The same applies to some, but not all, of the other method names in steps... You would make life much easier if you got rid of all the property stuff (some of it commented out and others not). Just use the methods and data attributes directly, it makes life so much easier. Here you find all the other code the principal are the same:http://pastebin.com/nYGEiXY4 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@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class learning
You are trying to use advanced features of Python, and they are not the right tool for what you're trying to do. Specifically, you're trying two things at the same time: 1. Properties, which allows method calls to look like simple variable access. 2. The __name__ special attribute on methods (reference: https://docs.python.org/2/reference/datamodel.html) to reflectively pick up a string that lets us get the name of a function. The problem is trying to use *both* of these features at the same time. It is self defeating. Here is a minimal example to demonstrate; ## class Test(object): @property def x(self): return 42 ## Consider the expression: Test2().x.__name__ This example is small enough that it should help to clarify what's going on. What did you want to happen? And what happens? Now look at: # class Test(object): def x(self): return 42 Test().x().__name__ # What do you expect to see when you run this, and why? The technical error in the first case is the same as the second. In short, I would strongly suggest you don't use @property, especially if you're learning the language. It's an advanced feature. In your particular case, you're getting into unnecessary trouble by using it. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class learning
On Fri, Jan 23, 2015 at 12:37 AM, jarod...@libero.it wrote: > Thanks for the help and patience! > It is a function on the class so I suppose for read that function list I > need self.steps Where I'm wrong? > @property > def steps(self): > return [ > > self.trimmomatic, > self.merge_trimmomatic_stats, > self.star, > self.picard_sort_sam, > self.rnaseqc, > self.wiggle, > self.cufflinks, > self.gq_seq_utils_exploratory_analysis_rnaseq > > ] > Do each of these elements in this list support the operations you're performing on any single step? That is, the code that you have here: def show(self): ftp="\n".join([str(idx + 1) + "- " + step.__name__ for idx, step in enumerate(self.steps)]) seems to assume that every step must have a '__name__' property. But do all of the steps that you've put in there support '__name__'? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class learning
> #@property > def show(self): > ftp="\n".join([str(idx + 1) + "- " + step.__name__ for idx, step in enumerate(self.steps)]) > Questions you should be asking yourself: What is self.steps? What type is it? In the case where this breaks with an error, what is self.steps then? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Class learning
Dear All How can gave the attributes __name__ to a function? class Foo(object): def __init__(self): steps = {} tmp = open("rnaseq.base.ini","rb") config.readfp(tmp) readsets = parse_illumina_readset_file("/home/mauro/Desktop/readset.csv") @property def steps(self): return [ self.one, self.two, self.fmit, ] def one(self): a = 5 return a def two(self): b = 5 return b def fmit(self): c = 7 return c #@property def show(self): ftp="\n".join([str(idx + 1) + "- " + step.__name__ for idx, step in enumerate(self.steps)]) print ftp It is working In [5]: F = Foo() In [6]: F.show() 1- one 2- two 3- fmit Why if I define the data in the same way I have this error? in () > 1 rna.show() in show(self) 261 #@property 262 def show(self): --> 263 ftp="\n".join([str(idx + 1) + "- " + step.__name__ for idx, step in enumerate(self.steps)]) 264 265 print ftp AttributeError: 'str' object has no attribute '__name__' Here you find all the other code the principal are the same:http://pastebin.com/nYGEiXY4 rna = Rnaseq() rna.show() thanks so much!! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor