From: Tim Johnson on
This is not a request for help but a request for comments:
Consider the following code and note that
1)The initializer uses the dictionary style of arguments
2)The check loop executes before all of the class variables
are declared
## --------------------------------------------------------------------
class formLoader():
def __init__(self,**kw):
self.fileName = None
self.record = None
self.string = None
## Uncomment below to see that only fileName, record and string
## are recorded by __dict__
#std.debug("self.__dict__",self.__dict__)
for k in kw.keys():
if k in self.__dict__:
self.__dict__[k] = kw[k]
else:
raise AttributeError("%s is not a class member. Use one of ('%s')"
% (repr(k),"', '".join(self.__dict__.keys())))
self.tokens = ["form","input","textarea","select","option","form"]
self.inputTypes = ["button","checkbox","file","hidden","image","password",
"radio","reset","submit","text"]
self.inputValTypes = ["file","hidden","password","text"]
self.colnames = [] ## case-insensitive column names
self.formIndexes = [] ## Index forms in outer list
## .....
## --------------------------------------------------------------------
Now if I were to code something like the following:
fml = formLoader(fileName=doc,tokens="tim")
## Note that `tokens' is passed as a named argument.
I get the following error message:
AttributeError: 'tokens' is not a class member. Use one of ('record',
'string', 'fileName')

I am not complaining! This is a good thing and a cool idiom.
Placing the the check loop after _only_ the keywords that I wanted to
allow _disallows_ any others.
I'd welcome comments - such as any other applications.

Always a newbie ....
--
Tim
tim(a)johnsons-web.com
http://www.akwebsoft.com
From: Steven D'Aprano on
On Thu, 29 Oct 2009 21:16:37 -0500, Tim Johnson wrote:

> This is not a request for help but a request for comments: Consider the
> following code and note that 1)The initializer uses the dictionary style
> of arguments 2)The check loop executes before all of the class variables
> are declared

Could you explain what problem you are trying to solve?


> class formLoader():

Idiomatic Python is to use CamelCase for classes.


> def __init__(self,**kw):
> self.fileName = None
> self.record = None
> self.string = None
> ## Uncomment below to see that only fileName, record
> ## and string are recorded by __dict__
> #std.debug("self.__dict__",self.__dict__)
> for k in kw.keys():

In recent versions of Python, this is best written as

for k in kw:

> if k in self.__dict__:
> self.__dict__[k] = kw[k]

Is your intention to ensure that the only keyword arguments allowed are
fileName, record and string?

Perhaps the easiest way to do that is:

for k in kw:
if k not in ('filename', 'record', 'string'):
raise Exception() # whatever...
setattr(self, k) = kw[k]



> else:
> raise AttributeError(

In my opinion, AttributeError is not appropriate here. Passing an invalid
parameter shouldn't raise the same error as failing an attribute look-up.
That's misleading and confusing.


> "%s is not a class member. Use one of ('%
> s')" % (repr(k),"', '".join
> (self.__dict__.keys())))

[Aside: eight space tabs are *WAY* too big for Usenet and email. You
should use four spaces, or even two, when posting here.]

It is idiomatic Python to use "instance attributes" to refer to
attributes attached to instances, and "class attributes" to refer to
those attached to classes. Talking about "class members" will confuse
Python developers who aren't also familiar with Java or C++. (I know it
confuses *me* -- are class members shared by all instances in a class, as
the name implies, or are they individual to the instance, as your code
implies?)

I also should point out that your trick will fail if you are using
__slots__.



> self.tokens =
["form","input","textarea","select","option","form"]
> self.inputTypes =
> ["button","checkbox","file","hidden","image","password",
> "radio","reset","submit","text"]
> self.inputValTypes = ["file","hidden","password","text"]
> self.colnames
> = [] ## case-insensitive column names
> self.formIndexes = [] ##
> Index forms in outer list


Is any of that relevant to the trick you are asking for comments for? If
not, why is it here?


> I'd welcome comments - such as any other applications.

Personally, I don't like it. The method signature makes it impossible to
tell what arguments are excepted, and it forces me to use keywords even
if I'd prefer to use positional arguments. I prefer to let the
interpreter check the arguments for me:


class FormLoader():
def __init__(self, fileName=None, record=None, string=None):
self.fileName = fileName
self.record = record
self.string = string

That's all it takes, and you get the benefit of a method signature that
makes it obvious what arguments it takes.



--
Steven
From: Ben Finney on
Steven D'Aprano <steve(a)REMOVE-THIS-cybersource.com.au> writes:

> On Thu, 29 Oct 2009 21:16:37 -0500, Tim Johnson wrote:
> > class formLoader():
>
> Idiomatic Python is to use CamelCase for classes.

Or rather: Instead of camelCase names, idiomatic Python is to use
TitleCase names.

--
\ “We are not gonna be great; we are not gonna be amazing; we are |
`\ gonna be *amazingly* amazing!” —Zaphod Beeblebrox, _The |
_o__) Hitch-Hiker's Guide To The Galaxy_, Douglas Adams |
Ben Finney
From: Ben Finney on
Ben Finney <ben+python(a)benfinney.id.au> writes:

> Steven D'Aprano <steve(a)REMOVE-THIS-cybersource.com.au> writes:
>
> > On Thu, 29 Oct 2009 21:16:37 -0500, Tim Johnson wrote:
> > > class formLoader():
> >
> > Idiomatic Python is to use CamelCase for classes.
>
> Or rather: Instead of camelCase names, idiomatic Python is to use
> TitleCase names.

Blah, my attempt at clarity just made it more confusing.

Instead of camelCase names, idiomatic Python is to use TitleCase names
for classes.

--
\ “Pinky, are you pondering what I'm pondering?” “I think so, |
`\ Brain, but how will we get a pair of Abe Vigoda's pants?” |
_o__) —_Pinky and The Brain_ |
Ben Finney
From: Steven D'Aprano on
On Fri, 30 Oct 2009 15:55:04 +1100, Ben Finney wrote:

> Steven D'Aprano <steve(a)REMOVE-THIS-cybersource.com.au> writes:
>
>> On Thu, 29 Oct 2009 21:16:37 -0500, Tim Johnson wrote:
>> > class formLoader():
>>
>> Idiomatic Python is to use CamelCase for classes.
>
> Or rather: Instead of camelCase names, idiomatic Python is to use
> TitleCase names.

Thank you for the correction. I always mix up those two.


--
Steven