# [PyQt] SIP newbie question (%MethodCode oddity)

Jim Crowell jaclists at mailbolt.com
Tue Jan 8 17:47:13 GMT 2008

```Hi all,

I'm fiddling with SIP 4.7.3 on XP for a non-Qt-related project. As a
learning exercise I tried porting the Fraction example at
<http://www.panix.com/~elflord/unix/siptute/> to SIP 4. Got it working
to a first approximation, but there's a bit that puzzles me: sipCpp
doesn't always get defined in %MethodCode blocks where it looks like it
should.

Here's the working .sip code for two methods:

float __float__();
%MethodCode
sipRes = (double)sipCpp->numerator() / sipCpp->denominator();
%End

%MethodCode
sipRes = new Fraction(*a0 + *a1);
%End

i.e. in the latter case sipCpp is not defined, but that same
self-as-c++-object pointer resides in a0. Can anyone tell me why?

Thanks,
-Jim C.

(complete .sip file below)

%Module Frac 0

#include <frac.h>
%End

int gcd(int, int);

class Fraction {
public:
Fraction(int , int=1 );
Fraction(const Fraction&);
int numerator() const;
int denominator() const;

SIP_PYOBJECT __str__();
%MethodCode
std::string res = sipCpp->toString();
sipRes = PyString_FromString( res.c_str() );
%End

float __float__();
%MethodCode
sipRes = (double)sipCpp->numerator() / sipCpp->denominator();
%End

%MethodCode
sipRes = new Fraction(*a0 + *a1);
%End

%MethodCode
Fraction f1( a1, 1 );
sipRes = new Fraction(*a0 + f1);
%End

Fraction __sub__( Fraction );
%MethodCode
sipRes = new Fraction(*a0 - *a1);
%End

Fraction __sub__( long );
%MethodCode
Fraction f1( a1, 1 );
sipRes = new Fraction(*a0 - f1);
%End

Fraction __mul__( Fraction );
%MethodCode
sipRes = new Fraction(*a0 * *a1);
%End

Fraction __mul__( long );
%MethodCode
Fraction f1( a1, 1 );
sipRes = new Fraction(*a0 * f1);
%End

Fraction __div__( Fraction );
%MethodCode
sipRes = new Fraction(*a0 / *a1);
%End

Fraction __div__( long );
%MethodCode
Fraction f1( a1, 1 );
sipRes = new Fraction(*a0 / f1);
%End

};

```