[Tutor] Tutor Digest, Vol 131, Issue 59 Order loop from methos on class (jarod_v6 at libero.it)
Dino Bektešević
ljetibo at gmail.com
Mon Jan 26 15:41:12 CET 2015
2015-01-26 14:54 GMT+01:00 jarod_v6 at libero.it <jarod_v6 at libero.it>:
> Thanks so much!! Now I understood it is a stupid thing!
> So, could you elpme to decompose the problem?
> I initiazlizate the class with the file to use so all the function have all
> the parameters that I need.
>
> At the moment I have in mind only to do this:
> ena = Rna()
> job1 = ena.trim()
> job2 = ena.align()
> It is no so elegant..
>
>
>
> Any suggestion?
>
Without seeing what exactly are you trying to do (I'm not a
chemist/biologist) I'm not sure. Any detail about what you're trying
to do would be helpful.
If you're bothered by having to call 2 methods except of 1 you could
always make another method that uses the two. It really depends on the
decision if you want to do the operations in place or not, that is
transform the original object through it's methods, or do you want to
return a new object of the same class.
In your example you write "job" as if you're dealing with threads. I
can't tell if you want job1 and job2 to be another different objects
(maybe of Rna class maybe not) or is it just that you don't understand
that you can also call a method on an object without having a variable
to hold a return value.
If you want to return another Rna object that is the same as the
original one, maybe something like this is what you need?
i.e.
import copy
class Rna():
def __init__(self):
self.trimmed = False
self.aligned = False
#NOT a part of the class
def trim(rna):
temp = copy.copy(rna)
temp.trimmed = True
return temp
#in interpreter
>>>> ena = Rna()
>>>> ena.trimmed
False
>>>> tena = trim(ena)
>>>> ena.trimmed
False
>>>> tena.trimmed
True
and both of ena and tena will be of class Rna. So you now have 2
different Rna objects.
You could also have it done in place, which saves memory and is more
natural and I'd recommend it:
class Rna():
def __init__(self):
self.trimmed=False
self.aligned=False
def trim(self):
self.trimmed=True
def align(self):
self.aligned=True
def DoBoth(self):
self.trim()
self.align()
print("Done!")
#in the interpreter
>>>> ena = Rna()
>>>> ena.trimmed
False
>>>> ena.aligned
False
>>>> ena.DoBoth()
Done!
>>>> ena.trimmed
True
>>>> ena.aligned
True
This way you can do both without having to constantly write them
yourself... you issue 1 call which then calls various other methods
you have on the instance itself....
Even that print isn't necessary, that's just there to show it
finished. You don't always have to have a variable to catch potential
output of a method. But maybe if you need/want to catch the output
maybe that output is an object of a different class(?) maybe it's a
tuple, or an array?
class Rna():
def __init__(self):
self.trimmed=False
self.aligned=False
def trim(self):
self.trimmed=True
def align(self):
self.aligned=True
def DoBoth(self):
self.trim()
self.align()
print("Done! We return an list of solutions:")
return [1,2,3,4,5,6,7,8,9]
#in the interpreter
>>>> ena = Rna()
>>>> solutions = ena.DoBoth()
Done! We return an list of solutions:
>>>> solutions
[1,2,3,4,5,6,7,8,9]
You could also add the solutions as an attribute to the object you
just did the calculation on:
def DoBoth(self):
self.trim()
self.align()
print("Done! We return an list of solutions:")
self.solutions = [1,2,3,4,5,6,7,8,9]
#in the interpreter
>>>> ena = Rna()
>>>> ena.DoBoth()
Done! We return an list of solutions:
>>>> ena.solutions
[1,2,3,4,5,6,7,8,9]
The better you describe WHAT you want and HOW you want it the better
people here will be able to help you....
Best of luck,
Dino
More information about the Tutor
mailing list