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

dave dave at csc.lsu.edu
Tue Jul 26 03:18:02 CEST 2011


Is it even possible to replace the implicit self argument of the initializer
by passing something else?  If so, what would be the syntax.

If you want to look at the code its all here:

https://www.cgran.org/browser/projects/ucla_zigbee_phy/trunk/src

The cc2420_txtest.py is in ./examples and the corresponding ieee802_15_4*.py
files are in ./python (lib contains C++ code accessed via SWIG).

I can probably puzzle it out with this info eventually, but if you want to
comment further feel free.

Thanks for your help

Dave



On Mon, 25 Jul 2011 10:26:11 +1000, Steven D'Aprano wrote
> 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
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list