Re: [Tutor] New class, how return value of x in interactive shell

2010-04-05 Thread Alan Gauld

"Vincent Davis"  wrote


class BString(XString):
   """ Biological string """

   _bstring_constructor = biostrings.BString

   @classmethod
   def new(cls, x):
   """ :param x: a (biological) string """
   res = cls(cls._bstring_constructor(conversion.py2ri(x)))
   _setExtractDelegators(res)
   return res


OK, I see what they are doing but as far as I can tell this could be 
put in a __new__ method just as easily which would retain the 
usual instantiation style.


Any gurus out there able to explain why they have used an 
explicit new() class method rather than __new__()?


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New class, how return value of x in interactive shell

2010-04-05 Thread Alan Gauld


"Vincent Davis"  wrote


Take a look at the repr and str methods:
http://docs.python.org/reference/datamodel.html#basic-customization

Ok so I am still a little confused, It seems that __str__ is used for 
print

and str()


That's right and repr() is used when evaluating the object, as at the >>> 
prompt.


So


print b # calls b.__str__()


whereas


b # calls b.__repr__()


HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New class, how return value of x in interactive shell

2010-04-05 Thread Vincent Davis
>
> That' a very strange idiom in Python.
> Can you show us the class definition?


It's a bioconductor extension to rpy2
http://www.bitbucket.org/lgautier/rpy2-bioc-extensions/overview/
I am trying to learn R and at they same time more about python and R
bioconductor packages. So no it is not homework but I am trying to learn
something. I am sure the answer is obvious when you know it :)
Here is the class, although it is obviously part of something bigger, you
can checkout the full code at bitbuckit.org

class BString(XString):
""" Biological string """

_bstring_constructor = biostrings.BString

@classmethod
def new(cls, x):
""" :param x: a (biological) string """
res = cls(cls._bstring_constructor(conversion.py2ri(x)))
_setExtractDelegators(res)
return res

*Vincent Davis
720-301-3003 *
vinc...@vincentdavis.net
 my blog  |
LinkedIn


On Mon, Apr 5, 2010 at 1:08 PM, Alan Gauld wrote:

> "Vincent Davis"  wrote
>
>> I am working an a open source project and would like to add feature to a
>>
>> class.
>> Current action:
>> in:>>>b = BString.new('I am a BString object')
>>
>
> That' a very strange idiom in Python.
> Can you show us the class definition?
>
>
>  out:  >>>b
>> in:>>>
>> in:>>>print(b)
>> out:  >>> 21-letter "BString" instance
>>  seq: I am a BString object
>>
>> What I would like is to be able to
>> in   >>>b
>> out >>>21-letter "BString" instance
>>  seq: I am a BString object
>>
>> I have 2 questions
>> 1, how do I do this?
>> 2, how does print know what to do?
>>
>
> If you look at your class definition that should become obvious.
> Are you sure this isn't a homework?
>
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New class, how return value of x in interactive shell

2010-04-05 Thread Alan Gauld
"Vincent Davis"  wrote 


I am working an a open source project and would like to add feature to a
class.
Current action:
in:>>>b = BString.new('I am a BString object')


That' a very strange idiom in Python.
Can you show us the class definition?


out:  >>>b
in:>>>
in:>>>print(b)
out:  >>> 21-letter "BString" instance
  seq: I am a BString object

What I would like is to be able to
in   >>>b
out >>>21-letter "BString" instance
  seq: I am a BString object

I have 2 questions
1, how do I do this?
2, how does print know what to do?


If you look at your class definition that should become obvious.
Are you sure this isn't a homework?


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New class, how return value of x in interactive shell

2010-04-05 Thread Vincent Davis
Take a look at the repr and str methods:
 http://docs.python.org/reference/datamodel.html#basic-customization

Ok so I am still a little confused, It seems that __str__ is used for print
and str()
byt what allows whats below. That is why does just entering the name of the
instance return print(b). I have tried a few combinations but not the right
one.
in   >>>b
out >>>21-letter "BString" instance
   seq: I am a BString object

*Vincent Davis
720-301-3003 *
vinc...@vincentdavis.net
 my blog  |
LinkedIn


On Mon, Apr 5, 2010 at 11:24 AM, Wayne Werner wrote:

>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New class, how return value of x in interactive shell

2010-04-05 Thread Wayne Werner
On Mon, Apr 5, 2010 at 12:08 PM, Vincent Davis wrote:

> I am working an a open source project and would like to add feature to a
> class.
> Current action:
> in:>>>b = BString.new('I am a BString object')
> out:  >>>b
> in:>>>
> in:>>>print(b)
> out:  >>> 21-letter "BString" instance
>seq: I am a BString object
>
> What I would like is to be able to
> in   >>>b
> out >>>21-letter "BString" instance
>seq: I am a BString object
>
> I have 2 questions
> 1, how do I do this?
> 2, how does print know what to do?
>
> I have a lot to learn so pointing me in the right direction or to
> documentation is as useful as the correct code.
>

Take a look at the repr and str methods:
 http://docs.python.org/reference/datamodel.html#basic-customization

HTH,
Wayne
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] New class, how return value of x in interactive shell

2010-04-05 Thread Vincent Davis
I am working an a open source project and would like to add feature to a
class.
Current action:
in:>>>b = BString.new('I am a BString object')
out:  >>>b
in:>>>
in:>>>print(b)
out:  >>> 21-letter "BString" instance
   seq: I am a BString object

What I would like is to be able to
in   >>>b
out >>>21-letter "BString" instance
   seq: I am a BString object

I have 2 questions
1, how do I do this?
2, how does print know what to do?

I have a lot to learn so pointing me in the right direction or to
documentation is as useful as the correct code.

Thanks

  *Vincent Davis
720-301-3003 *
vinc...@vincentdavis.net
 my blog  |
LinkedIn
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor