By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,941 Members | 1,789 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,941 IT Pros & Developers. It's quick & easy.

Alternative constructors naming convention

P: n/a
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
--
http://www.willmcgugan.com

Oct 11 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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 "alternative constructor".

My 2 cents.
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Oct 11 '06 #2

P: n/a
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_underscores for alternate constructors, not
CamelCase.

STeVe
Oct 11 '06 #3

P: n/a
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_underscores for alternate constructors, not
CamelCase.
Seems to be the consensus. I think I'll stick_to_it!
Oct 12 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.