How to test if Cython property is generator?
up vote
2
down vote
favorite
In IPython, I can see that a property of a Cython class is a generator by simply defining it and then calling:
%%cython
cdef class SomeCls:
property x:
def __get__(self):
yield 1
The call looks like
SomeCls().x
# prints <generator at 0x102f61ee8>
I am having trouble testing if that property is a generator:
import types
print(isinstance(SomeCls().x, types.GeneratorType))
# prints False
import inspect
print(inspect.isgeneratorfunction(SomeCls.x))
# prints False
How can I determine whether a property of a Cython class is a generator?
python types generator cython isinstance
add a comment |
up vote
2
down vote
favorite
In IPython, I can see that a property of a Cython class is a generator by simply defining it and then calling:
%%cython
cdef class SomeCls:
property x:
def __get__(self):
yield 1
The call looks like
SomeCls().x
# prints <generator at 0x102f61ee8>
I am having trouble testing if that property is a generator:
import types
print(isinstance(SomeCls().x, types.GeneratorType))
# prints False
import inspect
print(inspect.isgeneratorfunction(SomeCls.x))
# prints False
How can I determine whether a property of a Cython class is a generator?
python types generator cython isinstance
Avoid answering questions in comments.
– user1717828
Nov 21 at 20:56
Testing againsttypes.GeneratorType
works just fine when not using cython...
– Eric
Nov 21 at 21:00
Don't be picky about where you get help! As an experienced poster, I often answer with short comments. For a regular answer I spend more time, providing working examples and explanations.
– hpaulj
Nov 21 at 21:15
This doesn't fix the problem, but Cython now supports the standard@property
syntax (which I think is now preferred since it matches the rest of Python). It behaves exactly the same though with this.
– DavidW
Nov 21 at 22:37
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
In IPython, I can see that a property of a Cython class is a generator by simply defining it and then calling:
%%cython
cdef class SomeCls:
property x:
def __get__(self):
yield 1
The call looks like
SomeCls().x
# prints <generator at 0x102f61ee8>
I am having trouble testing if that property is a generator:
import types
print(isinstance(SomeCls().x, types.GeneratorType))
# prints False
import inspect
print(inspect.isgeneratorfunction(SomeCls.x))
# prints False
How can I determine whether a property of a Cython class is a generator?
python types generator cython isinstance
In IPython, I can see that a property of a Cython class is a generator by simply defining it and then calling:
%%cython
cdef class SomeCls:
property x:
def __get__(self):
yield 1
The call looks like
SomeCls().x
# prints <generator at 0x102f61ee8>
I am having trouble testing if that property is a generator:
import types
print(isinstance(SomeCls().x, types.GeneratorType))
# prints False
import inspect
print(inspect.isgeneratorfunction(SomeCls.x))
# prints False
How can I determine whether a property of a Cython class is a generator?
python types generator cython isinstance
python types generator cython isinstance
asked Nov 21 at 20:55
user1717828
3,23151637
3,23151637
Avoid answering questions in comments.
– user1717828
Nov 21 at 20:56
Testing againsttypes.GeneratorType
works just fine when not using cython...
– Eric
Nov 21 at 21:00
Don't be picky about where you get help! As an experienced poster, I often answer with short comments. For a regular answer I spend more time, providing working examples and explanations.
– hpaulj
Nov 21 at 21:15
This doesn't fix the problem, but Cython now supports the standard@property
syntax (which I think is now preferred since it matches the rest of Python). It behaves exactly the same though with this.
– DavidW
Nov 21 at 22:37
add a comment |
Avoid answering questions in comments.
– user1717828
Nov 21 at 20:56
Testing againsttypes.GeneratorType
works just fine when not using cython...
– Eric
Nov 21 at 21:00
Don't be picky about where you get help! As an experienced poster, I often answer with short comments. For a regular answer I spend more time, providing working examples and explanations.
– hpaulj
Nov 21 at 21:15
This doesn't fix the problem, but Cython now supports the standard@property
syntax (which I think is now preferred since it matches the rest of Python). It behaves exactly the same though with this.
– DavidW
Nov 21 at 22:37
Avoid answering questions in comments.
– user1717828
Nov 21 at 20:56
Avoid answering questions in comments.
– user1717828
Nov 21 at 20:56
Testing against
types.GeneratorType
works just fine when not using cython...– Eric
Nov 21 at 21:00
Testing against
types.GeneratorType
works just fine when not using cython...– Eric
Nov 21 at 21:00
Don't be picky about where you get help! As an experienced poster, I often answer with short comments. For a regular answer I spend more time, providing working examples and explanations.
– hpaulj
Nov 21 at 21:15
Don't be picky about where you get help! As an experienced poster, I often answer with short comments. For a regular answer I spend more time, providing working examples and explanations.
– hpaulj
Nov 21 at 21:15
This doesn't fix the problem, but Cython now supports the standard
@property
syntax (which I think is now preferred since it matches the rest of Python). It behaves exactly the same though with this.– DavidW
Nov 21 at 22:37
This doesn't fix the problem, but Cython now supports the standard
@property
syntax (which I think is now preferred since it matches the rest of Python). It behaves exactly the same though with this.– DavidW
Nov 21 at 22:37
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Why doesn't the usual way work?
First, as you already probably know, there is no difference between inspect.isgeneratorfunction(...)
and isinstance(..., types.GeneratorType)
- the inspect-module just calls isinstance(..., types.GeneratorType)
.
On the other hand, types.GeneratorType
is defined as
def _g():
yield 1
GeneratorType = type(_g())
CPython uses PyGenObject (here code, here documentation) for generators, there is no fancy logic for the comparison as for some ABC-classes, so the isinstance
will boil down to comparing the C-object types.
However, Cython returns a __pyx_CoroutineObject
for generators (just check the cythonized code to see)
typedef PyObject *(*__pyx_coroutine_body_t)(PyObject *, PyThreadState *, PyObject *);
typedef struct {
PyObject_HEAD
__pyx_coroutine_body_t body;
PyObject *closure;
...
int resume_label;
char is_running;
} __pyx_CoroutineObject;
which has nothing to do with PyGenObject
as far as isinstance
is concerned - it doesn't really care whether generator
is in the name of the type (but for us humans it can be really puzzling, because type(obj)
says "generator").
So you will have to roll out your own version of isgenerator
, which takes also Cython-"generators" into account. There are many ways, for example
%%cython
def _f():
yield 1
CyGeneratorType = type(_f())
def iscygenerator(o):
return isinstance(o, CyGeneratorType)
and now:
import inspect
def isgenerator(o):
return inspect.isgenerator(o) or iscygenerator(o)
isgenerator(SomeCls().x) # True
iscygenerator(SomeCls().x) # True
inspect.isgenerator(SomeCls().x) # False
This answered the question in the MWE that I posted, but unfortunately it didn't solve my problem (determining the type of this Spacy attribute). However, I was able to use your example to setSpacyGeneratorType = type(doc.subtree)
and then test against that type, like you showed. This mostly teaches me how little I know about types in Cython. Thanks for your help!
– user1717828
Nov 27 at 18:45
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Why doesn't the usual way work?
First, as you already probably know, there is no difference between inspect.isgeneratorfunction(...)
and isinstance(..., types.GeneratorType)
- the inspect-module just calls isinstance(..., types.GeneratorType)
.
On the other hand, types.GeneratorType
is defined as
def _g():
yield 1
GeneratorType = type(_g())
CPython uses PyGenObject (here code, here documentation) for generators, there is no fancy logic for the comparison as for some ABC-classes, so the isinstance
will boil down to comparing the C-object types.
However, Cython returns a __pyx_CoroutineObject
for generators (just check the cythonized code to see)
typedef PyObject *(*__pyx_coroutine_body_t)(PyObject *, PyThreadState *, PyObject *);
typedef struct {
PyObject_HEAD
__pyx_coroutine_body_t body;
PyObject *closure;
...
int resume_label;
char is_running;
} __pyx_CoroutineObject;
which has nothing to do with PyGenObject
as far as isinstance
is concerned - it doesn't really care whether generator
is in the name of the type (but for us humans it can be really puzzling, because type(obj)
says "generator").
So you will have to roll out your own version of isgenerator
, which takes also Cython-"generators" into account. There are many ways, for example
%%cython
def _f():
yield 1
CyGeneratorType = type(_f())
def iscygenerator(o):
return isinstance(o, CyGeneratorType)
and now:
import inspect
def isgenerator(o):
return inspect.isgenerator(o) or iscygenerator(o)
isgenerator(SomeCls().x) # True
iscygenerator(SomeCls().x) # True
inspect.isgenerator(SomeCls().x) # False
This answered the question in the MWE that I posted, but unfortunately it didn't solve my problem (determining the type of this Spacy attribute). However, I was able to use your example to setSpacyGeneratorType = type(doc.subtree)
and then test against that type, like you showed. This mostly teaches me how little I know about types in Cython. Thanks for your help!
– user1717828
Nov 27 at 18:45
add a comment |
up vote
2
down vote
accepted
Why doesn't the usual way work?
First, as you already probably know, there is no difference between inspect.isgeneratorfunction(...)
and isinstance(..., types.GeneratorType)
- the inspect-module just calls isinstance(..., types.GeneratorType)
.
On the other hand, types.GeneratorType
is defined as
def _g():
yield 1
GeneratorType = type(_g())
CPython uses PyGenObject (here code, here documentation) for generators, there is no fancy logic for the comparison as for some ABC-classes, so the isinstance
will boil down to comparing the C-object types.
However, Cython returns a __pyx_CoroutineObject
for generators (just check the cythonized code to see)
typedef PyObject *(*__pyx_coroutine_body_t)(PyObject *, PyThreadState *, PyObject *);
typedef struct {
PyObject_HEAD
__pyx_coroutine_body_t body;
PyObject *closure;
...
int resume_label;
char is_running;
} __pyx_CoroutineObject;
which has nothing to do with PyGenObject
as far as isinstance
is concerned - it doesn't really care whether generator
is in the name of the type (but for us humans it can be really puzzling, because type(obj)
says "generator").
So you will have to roll out your own version of isgenerator
, which takes also Cython-"generators" into account. There are many ways, for example
%%cython
def _f():
yield 1
CyGeneratorType = type(_f())
def iscygenerator(o):
return isinstance(o, CyGeneratorType)
and now:
import inspect
def isgenerator(o):
return inspect.isgenerator(o) or iscygenerator(o)
isgenerator(SomeCls().x) # True
iscygenerator(SomeCls().x) # True
inspect.isgenerator(SomeCls().x) # False
This answered the question in the MWE that I posted, but unfortunately it didn't solve my problem (determining the type of this Spacy attribute). However, I was able to use your example to setSpacyGeneratorType = type(doc.subtree)
and then test against that type, like you showed. This mostly teaches me how little I know about types in Cython. Thanks for your help!
– user1717828
Nov 27 at 18:45
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Why doesn't the usual way work?
First, as you already probably know, there is no difference between inspect.isgeneratorfunction(...)
and isinstance(..., types.GeneratorType)
- the inspect-module just calls isinstance(..., types.GeneratorType)
.
On the other hand, types.GeneratorType
is defined as
def _g():
yield 1
GeneratorType = type(_g())
CPython uses PyGenObject (here code, here documentation) for generators, there is no fancy logic for the comparison as for some ABC-classes, so the isinstance
will boil down to comparing the C-object types.
However, Cython returns a __pyx_CoroutineObject
for generators (just check the cythonized code to see)
typedef PyObject *(*__pyx_coroutine_body_t)(PyObject *, PyThreadState *, PyObject *);
typedef struct {
PyObject_HEAD
__pyx_coroutine_body_t body;
PyObject *closure;
...
int resume_label;
char is_running;
} __pyx_CoroutineObject;
which has nothing to do with PyGenObject
as far as isinstance
is concerned - it doesn't really care whether generator
is in the name of the type (but for us humans it can be really puzzling, because type(obj)
says "generator").
So you will have to roll out your own version of isgenerator
, which takes also Cython-"generators" into account. There are many ways, for example
%%cython
def _f():
yield 1
CyGeneratorType = type(_f())
def iscygenerator(o):
return isinstance(o, CyGeneratorType)
and now:
import inspect
def isgenerator(o):
return inspect.isgenerator(o) or iscygenerator(o)
isgenerator(SomeCls().x) # True
iscygenerator(SomeCls().x) # True
inspect.isgenerator(SomeCls().x) # False
Why doesn't the usual way work?
First, as you already probably know, there is no difference between inspect.isgeneratorfunction(...)
and isinstance(..., types.GeneratorType)
- the inspect-module just calls isinstance(..., types.GeneratorType)
.
On the other hand, types.GeneratorType
is defined as
def _g():
yield 1
GeneratorType = type(_g())
CPython uses PyGenObject (here code, here documentation) for generators, there is no fancy logic for the comparison as for some ABC-classes, so the isinstance
will boil down to comparing the C-object types.
However, Cython returns a __pyx_CoroutineObject
for generators (just check the cythonized code to see)
typedef PyObject *(*__pyx_coroutine_body_t)(PyObject *, PyThreadState *, PyObject *);
typedef struct {
PyObject_HEAD
__pyx_coroutine_body_t body;
PyObject *closure;
...
int resume_label;
char is_running;
} __pyx_CoroutineObject;
which has nothing to do with PyGenObject
as far as isinstance
is concerned - it doesn't really care whether generator
is in the name of the type (but for us humans it can be really puzzling, because type(obj)
says "generator").
So you will have to roll out your own version of isgenerator
, which takes also Cython-"generators" into account. There are many ways, for example
%%cython
def _f():
yield 1
CyGeneratorType = type(_f())
def iscygenerator(o):
return isinstance(o, CyGeneratorType)
and now:
import inspect
def isgenerator(o):
return inspect.isgenerator(o) or iscygenerator(o)
isgenerator(SomeCls().x) # True
iscygenerator(SomeCls().x) # True
inspect.isgenerator(SomeCls().x) # False
answered Nov 22 at 6:44
ead
12k22455
12k22455
This answered the question in the MWE that I posted, but unfortunately it didn't solve my problem (determining the type of this Spacy attribute). However, I was able to use your example to setSpacyGeneratorType = type(doc.subtree)
and then test against that type, like you showed. This mostly teaches me how little I know about types in Cython. Thanks for your help!
– user1717828
Nov 27 at 18:45
add a comment |
This answered the question in the MWE that I posted, but unfortunately it didn't solve my problem (determining the type of this Spacy attribute). However, I was able to use your example to setSpacyGeneratorType = type(doc.subtree)
and then test against that type, like you showed. This mostly teaches me how little I know about types in Cython. Thanks for your help!
– user1717828
Nov 27 at 18:45
This answered the question in the MWE that I posted, but unfortunately it didn't solve my problem (determining the type of this Spacy attribute). However, I was able to use your example to set
SpacyGeneratorType = type(doc.subtree)
and then test against that type, like you showed. This mostly teaches me how little I know about types in Cython. Thanks for your help!– user1717828
Nov 27 at 18:45
This answered the question in the MWE that I posted, but unfortunately it didn't solve my problem (determining the type of this Spacy attribute). However, I was able to use your example to set
SpacyGeneratorType = type(doc.subtree)
and then test against that type, like you showed. This mostly teaches me how little I know about types in Cython. Thanks for your help!– user1717828
Nov 27 at 18:45
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420354%2fhow-to-test-if-cython-property-is-generator%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Avoid answering questions in comments.
– user1717828
Nov 21 at 20:56
Testing against
types.GeneratorType
works just fine when not using cython...– Eric
Nov 21 at 21:00
Don't be picky about where you get help! As an experienced poster, I often answer with short comments. For a regular answer I spend more time, providing working examples and explanations.
– hpaulj
Nov 21 at 21:15
This doesn't fix the problem, but Cython now supports the standard
@property
syntax (which I think is now preferred since it matches the rest of Python). It behaves exactly the same though with this.– DavidW
Nov 21 at 22:37