Still the __new__ hell ...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bruno Desthuilliers

    #16
    Re: Still the __new__ hell ...

    Paulo da Silva a écrit :
    Bruno Desthuilliers escreveu:
    >Paulo da Silva a écrit :
    ...
    >
    >>class MyDate(date):
    >> def __new__(cls,yea r,month=None,da y=None):
    >> if type(year) is str:
    >And what if it's a unicode string ?
    >The correct idiom here is:
    > if isinstance(year , basestring):
    >>
    >
    >
    Thanks.
    If I do type(year) I get either int or str (may be unicode for unicode
    strings) but never anything like basestring.
    It's an abstract class.
    As a relatively inexperient
    in python, how could I know that a 'string' is an instance of
    basestring?
    By reading this newsgroup ?-)
    x=u"xxxxxx"; help(x) says this is unicode based on
    basestring but help does not "work" for x="xxxxxxxx".
    help(str)
    May be the python tutorial should be upgraded to include these new
    concepts. Also covering the basics of __init__/__new__ (why have both?)
    It's a long story. But it's a GoodThing(tm) IMVHO.

    Comment

    • Duncan Booth

      #17
      Re: Still the __new__ hell ...

      Paulo da Silva <psdasilvaX@eso tericaX.ptXwrot e:
      As a relatively inexperient
      in python, how could I know that a 'string' is an instance of
      basestring?
      I think a good tip for anyone learning Python (or upgrading from an earlier
      version) is to do:

      dir(__builtins_ _)

      at the interactive prompt and read through the list it produces working out
      what each builtin does. Some of them you can ignore(1), some you can file
      away as interesting to know they exist but you don't need to know the
      details today(2), and others are indispensible(3 ).

      Do that and you'll find basestring (category 2 until you needed to ask your
      question) between apply (category 1) and bool (category 3).

      Comment

      • Gabriel Genellina

        #18
        Re: Still the __new__ hell ...

        En Wed, 21 Mar 2007 07:51:32 -0300, Bruno Desthuilliers
        <bruno.42.desth uilliers@wtf.we bsiteburo.oops. comescribió:
        Paulo da Silva a écrit :
        >As a relatively inexperient
        >in python, how could I know that a 'string' is an instance of
        >basestring?
        >
        By reading this newsgroup ?-)
        Or asking Python itself:
        >>type("any string").mro()
        [<type 'str'>, <type 'basestring'>, <type 'object'>]

        --
        Gabriel Genellina

        Comment

        • Paulo da Silva

          #19
          Re: Still the __new__ hell ...

          Gabriel Genellina escreveu:
          En Wed, 21 Mar 2007 07:51:32 -0300, Bruno Desthuilliers
          <bruno.42.desth uilliers@wtf.we bsiteburo.oops. comescribió:
          >
          >Paulo da Silva a écrit :
          >>As a relatively inexperient
          >>in python, how could I know that a 'string' is an instance of
          >>basestring?
          >>
          >By reading this newsgroup ?-)
          >
          Or asking Python itself:
          >
          >>>type("any string").mro()
          [<type 'str'>, <type 'basestring'>, <type 'object'>]
          >
          --Gabriel Genellina
          >
          Good! ;-)
          Paulo

          Comment

          Working...