AttributeError : object has no attribute

2,940 views
Skip to first unread message

King

unread,
Sep 26, 2010, 12:57:57 PM9/26/10
to cython-users
Hi,

I am new to cython and my c/c++ are skills are limited. I am trying to
get in cdef/cpdef class programming for fast classes.

Here is my class. It's a slightly modified version then available
here:
http://wiki.cython.org/EarlyBindingForSpeed

cdef class Rectangle:
"""A simple Rectangle class"""

# Declare class members
cdef double x0, y0, x1, y1

def __init__(self, double x0=0, double y0=0, double x1=0, double
y1=0):
self.x0 = x0
self.y0 = y0
self.x1 = x1
self.y1 = y1

cpdef double area(self):
""" Calculate area of rectangle """
# Declare local variables
cdef double area
# Calculate area
area = (self.x1 - self.x0) * (self.y1 - self.y0)
if area < 0:
area = -area
# Return area
return area

Module (test.pyd) is compiled successfully. When I run this code:

rect = test.Rectangle(1,2,3,4)
print rect.area()
print rect.x1

'area' is printed correctly but print 'rect.x1' is producing an error:
AttributeError: 'test.Rectangle' object has no attribute 'x1'

Any clues?

XP 32
python 2.6.5
cython 0.11.2

Lisandro Dalcin

unread,
Sep 26, 2010, 5:43:03 PM9/26/10
to cython-users

x0,x1,etc. are declared "private", you cannot access them outside the
methods of Rectangle. If you do what do access them, then:

   cdef public double x0, y0, x1, y1

i.e. add "public" after "cdef"

> XP 32
> python 2.6.5
> cython 0.11.2
>

BTW, please upgrade your Cython to 0.13.


--
Lisandro Dalcin
---------------
CIMEC (INTEC/CONICET-UNL)
Predio CONICET-Santa Fe
Colectora RN 168 Km 472, Paraje El Pozo
Tel: +54-342-4511594 (ext 1011)
Tel/Fax: +54-342-4511169

King

unread,
Sep 26, 2010, 10:59:14 PM9/26/10
to cython-users
Thanks,

It's working fine with 'public' keyword. BTW, I did upgrade to 0.13
and now I am getting an error saying:

"""
cdef class Rectangle:
^
------------------------------------------------------------

D:\Cython\cython_test\test.py:18:5: Syntax error in simple statement
list
building 'test' extension
"""

Any pointers?

Prashant




On Sep 27, 2:43 am, Lisandro Dalcin <dalc...@gmail.com> wrote:

Lisandro Dalcin

unread,
Sep 27, 2010, 9:41:30 AM9/27/10
to cython...@googlegroups.com
On 26 September 2010 23:59, King <anima...@gmail.com> wrote:
> Thanks,
>
> It's working fine with 'public' keyword. BTW, I did upgrade to 0.13
> and now I am getting an error saying:
>
> """
> cdef class Rectangle:
>    ^
> ------------------------------------------------------------
>
> D:\Cython\cython_test\test.py:18:5: Syntax error in simple statement
> list
> building 'test' extension
> """
>
> Any pointers?
>
> Prashant
>

Could you attach the full file?

King

unread,
Sep 27, 2010, 1:13:10 PM9/27/10
to cython-users
Here is the file

cdef class Rectangle:
"""A simple Rectangle class"""

# Declare public class members
cdef public double x0, y0, x1, y1

def __init__(self, double x0=0, double y0=0, double x1=0, double
y1=0):
self.x0 = x0
self.y0 = y0
self.x1 = x1
self.y1 = y1

cpdef double area(self):
""" Calculate area of rectangle """
# Declare local variables
cdef double area
# Calculate area
area = (self.x1 - self.x0) * (self.y1 - self.y0)
if area < 0:
area = -area
# Return area
return area

#################################
Here is setup.py

import sys
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

# These are the modules we need to compile usning cython.
ext_modules=[
Extension("test1", ["test1.py"]),
]

setup(
name = 'CythonTest',
version='0.01',
description='Nothing here',
author='Prashant',
author_email='Nope',
url='www.cython.org',
cmdclass = {'build_ext': build_ext},
ext_modules = ext_modules,
)

########################
And finally on command prompt:
python setup.py build_ext -c mingw32 --inplace

On Sep 27, 6:41 pm, Lisandro Dalcin <dalc...@gmail.com> wrote:

Robert Bradshaw

unread,
Sep 27, 2010, 1:28:22 PM9/27/10
to cython...@googlegroups.com

To use keywords like "cdef" your extension needs to be .pyx. (This is
so that .py files can have 100% Python compatibility.)

- Robert

King

unread,
Sep 27, 2010, 1:58:49 PM9/27/10
to cython-users
Yes...!!!

It's working.

On Sep 27, 10:28 pm, Robert Bradshaw <rober...@math.washington.edu>
wrote:
Reply all
Reply to author
Forward
0 new messages