self.__dict__ tricks
Tim Johnson
tim at johnsons-web.com
Fri Oct 30 12:16:29 EDT 2009
On 2009-10-30, Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> wrote:
>
> Could you explain what problem you are trying to solve?
>
>
>> class formLoader():
Hi Steve
In a nutshell:
The 'problem' is to parse a form in such a way that tags which are to
be modified are represented as dictionaries. The 'grunt' work is
done. This class will probably grow with time.
> Idiomatic Python is to use CamelCase for classes.
Can you point me to a discussion on Idiomatic Python, CamelCase and
other matters?
> Is your intention to ensure that the only keyword arguments allowed are
> fileName, record and string?
Correct.
> 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]
Understood. A better 'trick'
>> 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.
What error class or other approach do you recommend?
> [Aside: eight space tabs are *WAY* too big for Usenet and email. You
> should use four spaces, or even two, when posting here.]
Yikes! I just starting using vim with slrn again. Will take care of
that.
> 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?)
Thanks for that. C++ corrupted me.
> I also should point out that your trick will fail if you are using
> __slots__.
??. Will research that. Elaborate if you wish.
>> self.tokens =
> ["form","input","textarea","select","option","form"]
<....>
>
> Is any of that relevant to the trick you are asking for comments for? If
> not, why is it here?
It was there to illustrate my edification of the usage of self.__dict__
>> 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:
Using your tuple example would clarify things. The method signature
then becomes the tuple. I.E
#<steve sayeth>
if k not in ('filename', 'record', 'string'):
# handle here
If the class grows - and I expect it will - I'd prefer to stick with
the keywords approach. That approach also allows me to use a
dictionary to initialize the object.
Thanks for the input. Very helpful.
- and always a newbie -
--
Tim
tim at johnsons-web.com
http://www.akwebsoft.com
More information about the Python-list
mailing list