Alternative constructors naming convention

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Will McGugan

    #1

    Alternative constructors naming convention

    Hi,

    Is there a naming convention regarding alternative constructors? ie
    static methods where __new__ is called explicity. I use lower_case for
    methods in general, but thought maybe CamelCase would be better for
    alternative contstructors to distinguish them from methods...

    So which is better?

    c = Color.FromHtml( r, g, b)

    c = Color.from_html (r, g, b)


    Will McGugan
    --


  • Bruno Desthuilliers

    #2
    Re: Alternative constructors naming convention

    Will McGugan wrote:
    Hi,
    >
    Is there a naming convention regarding alternative constructors? ie
    static methods where __new__ is called explicity. I use lower_case for
    methods in general, but thought maybe CamelCase would be better for
    alternative contstructors to distinguish them from methods...
    Well... They are still methods, aren't they ?-)

    I don't remember any specific guideline, but FWIW, dict.from_keys( ) is
    an "alternativ e constructor".

    My 2 cents.
    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in 'onurb@xiludom. gro'.split('@')])"

    Comment

    • Steven Bethard

      #3
      Re: Alternative constructors naming convention

      Will McGugan wrote:
      Is there a naming convention regarding alternative constructors? ie
      static methods where __new__ is called explicity.
      Are you really using staticmethod and calling __new__? It's often much
      easier to use classmethod, e.g.::

      class Color(object):
      ...
      @classmethod
      def from_html(cls, r, g, b):
      ...
      # convert r, g, b to normal constructor args
      ...
      # call normal constructor
      return cls(...)

      And FWIW, I use lower_with_unde rscores for alternate constructors, not
      CamelCase.

      STeVe

      Comment

      • Will McGugan

        #4
        Re: Alternative constructors naming convention

        Steven Bethard wrote:
        Are you really using staticmethod and calling __new__? It's often much
        easier to use classmethod, e.g.::
        >
        class Color(object):
        ...
        @classmethod
        def from_html(cls, r, g, b):
        ...
        # convert r, g, b to normal constructor args
        ...
        # call normal constructor
        return cls(...)
        >
        I could use that for some things, but often I can avoid an intermediate
        step by bypassing the class constructor altogether...
        And FWIW, I use lower_with_unde rscores for alternate constructors, not
        CamelCase.
        Seems to be the consensus. I think I'll stick_to_it!

        Comment

        Working...