[Tutor] Don't understand this class/constructor call syntax

Steven D'Aprano steve at pearwood.info
Mon Jul 25 02:26:11 CEST 2011


dave wrote:
> I was dimly aware of the functioning of booleans, but I see now that it
> doesn't specify an actual boolean type.  Still, the code confuses me.  Is the
> usage of pad_for_usrp consistent with it being treated as a boolean?  Why
> would the entire self reference be transmitted then?

Parameter passing in Python is fast -- the object (which may be large) 
is not copied unless you explicitly make a copy. So it is no faster to 
pass a big, complex object than a lightweight object like True or False.

(Implementation note: in CPython, the main Python implementation which 
you almost certainly are using, objects live in the heap and are passed 
around as pointers.)

The code you show isn't very illuminating as far as pad_for_usrp goes. 
All that happens is that it gets stored as an attribute, then later gets 
passed on again to another function or class:


> class ieee802_15_4_mod_pkts(gr.hier_block2):
...
>         self.pad_for_usrp = pad_for_usrp

>     def send_pkt(self, seqNr, addressInfo, payload='', eof=False):
...
>             pkt = make_ieee802_15_4_packet(FCF,
>                                            seqNr,
>                                            addressInfo,
>                                            payload,
>                                            self.pad_for_usrp)

So it's *consistent* with being used as a bool, or anything else for 
that matter! I expect that make_ieee802_15_4_packet may be the thing 
that actually does something useful with pad_for_usrp.

Another thing to look for is the transmit_path class itself. If it has a 
__len__, __bool__ or __nonzero__ method, then it has customized the way 
it appears as a boolean. If it has none of those methods, then it will 
always be considered true-valued, and I can't imagine why it is being 
used as pad_for_usrp instead of just passing True.

But without looking at the rest of the code, I can't really tell for sure.




-- 
Steven



More information about the Tutor mailing list