Referring to class methods in class attributes

mk mrkafk at gmail.com
Wed Feb 17 19:38:10 CET 2010


Stephen Hansen wrote:

> You don't have to (and can't) refer to the class within the body. Class 
> statements are sort of... odd. They are code which is directly executed, 
> and the results are then passed into a metaclass/type/whatever and a 
> class object is created. While within the class body, the class doesn't 
> exist yet.
> 
> But you don't need it to.
> 
> Just do:
> 
>    'internal_date': print_internal_date
> 
> The 'def' is in the same local scope as 'tagdata' is.

Thanks, that worked. But in order to make it work I had to get rid of 
'self' in print_internal_date signature, bc all other functions in 
tagdata have only a single argument:

class PYFileInfo(FileInfo):
     'python file properties'

     def print_internal_date(filename):
...

     tagdata = {'compiled_fname': lambda x: x + 'c',
                 'size': os.path.getsize,
                 'internal_date': print_internal_date
             }

That looks weird: a method with no 'self'. Hmm that is probably 
seriously wrong.

This obviously means no other method can call it like 
self.print_internal_date(), because self would get passed as first 
argument, yes?

I checked that print_internal_date can be called on an instance, so the 
same method can be seen as:

- class method -- when used in local scope definition like tagdata, or
- instance method -- when called from an instance or from self?

It should be called ______print_internal_date really. ;-)

I wonder if I'm not trying to make Python things it shouldn't be doing, 
but it's the problem at hand that is leading me into this conundrum: all 
other functions for tagdata use single arguments. I should probably code 
around that anyway..

Regards,
mk






More information about the Python-list mailing list