[Tutor] Using new style classes and __slots__

Liam Clarke ml.cyresse at gmail.com
Sun Sep 25 10:34:12 CEST 2005

> Oh wait, I get it - you are passing bound methods to property(). So um is bound to the instance when you access self.getA. Use Strange.getA instead of self.getA, then use the normal signatures.

Ahh... I get it.

> But I don't get why you are doing this at all?

Because some of the objects I'm working with have 65 attributes, each
one needs to be typechecked and then passed down the line. As the
knowledge of the file structure I'm reading is not complete, as it
changes I'll have to update it. At the moment, what I'm doing is I
have a  docstring like so -
	Type: Track Item
	Tuple format:
		[0]   header_id (4string)
		[1]   header_length (int)
		[2]   total_length (int)             		<-- (Of child strings + header)
		[3]   num_of_strings (int)       		<-- (Above child strings)
		... ...
		[62] unknown (int)
		[63] unknown (int)
		[64] unknown (int)

and a struct string "4s6ih2c7i2h12iq2c3h10iq17i"

Now, as knowledge of the format changes, so does that tuple format and
struct string.
I have a convenience function which parses my docstring for each class
and creates a module dictionary of attribute name, type and length if
applicable. It can also create a struct string based on those types. I
can then call a generic set for all those attributes, which will pull
the type out of the dict and check it, and also acts as a repository
for attribute names.

If the file structure changes, I simply update my doc string, call my
convenience function, and my name:type dictionary is updated, as is my
struct string.

Alternatively, I can manually add header_id = property(..) and
manually update the struct string and the doc string when something

However, on reflection, I just figured out that it'll be simpler using
__setattr__ for this one, as I won't be able to get the attribute name
using property() unless I use a function factory to generate 127
functions and use func.__name__, when using __setattr__ and a
dictionary lookup is going to be much simpler.

But, it's good to know how to use property. I can think of a couple of
uses it for it. I wasn't thinking far enough ahead in this case, so
please forgive my emailed meanderings.

> It is passing self twice, because you are using a bound method as the property method
> rather than an unbound method.

Erk. That seems obvious in hindsight, like a cryptic crossword answer.
Once again, my lack of experience with this sorta stuff comes up.

I've never really dealt with indepth oo stuff before, so this has all
been a gigantic learning curve. Thanks for your help on this, Kent.

>> This is a hassle for me because I'm a lazy typist, so I've been using
>> setattr() to pull attribute names out of a list. And the first
>> argument setattr() requires is an object, and self doesn't work
>> outside of a method, and using the class name leads to no attribute
>> being set.

>I don't understand this at all, can you give an example?

Basically, I just found that outside methods, "self" gives a
NameError. The examples are all needless now, anyway.


Liam Clarke

More information about the Tutor mailing list