473,326 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Class methods in Python/C?

Hi folks

I've been doing some looking around, but have been unable to find out
how to implement class methods on Python objects written in C. "Why are
you using C?" you ask?

Yeah, so do I. However, I need to provide bindings for an application
that Python is embedded into, and thanks to Qt the bindings are going to
be both simple and quite powerful. However, I need a way to do class
methods...

If anybody has any tips on this, It'd be much appreciated.

--
Craig Ringer

Jul 18 '05 #1
2 2066
Craig Ringer wrote:
Hi folks

I've been doing some looking around, but have been unable to find out
how to implement class methods on Python objects written in C. "Why are
you using C?" you ask?

Yeah, so do I. However, I need to provide bindings for an application
that Python is embedded into, and thanks to Qt the bindings are going to
be both simple and quite powerful. However, I need a way to do class
methods...

If anybody has any tips on this, It'd be much appreciated.


You probably want to look at staticmethod(). (classmethod() is slightly
different, and probably not what you want. In fact, classmethod() is practically
*never* what you want. Guido wrote it himself, and even he ended up not using it)

class Demo(object):
def some_static_method(some_arg_list):
pass
some_static_method = staticmethod(some_static_method)

Then call the function as:
Demo.some_static_method(some_args)

In Py2.4, the method definition can be collapsed to:

class Demo(object):
@staticmethod
def some_static_method(some_arg_list):
pass

Cheers,
Nick.
Jul 18 '05 #2
On Tue, 2004-11-30 at 20:39, Nick Coghlan wrote:
You probably want to look at staticmethod(). (classmethod() is slightly
different, and probably not what you want. In fact, classmethod() is practically
*never* what you want. Guido wrote it himself, and even he ended up not using it)


Hmm, I've always rather favoured class methods myself. However, that's
not the point - the same question can apply just as well to static
methods. I know how to construct both in Python, though I rarely use
static methods, only class methods.

What I was after is a way to define a static method in a C extension
module.

I just found it - in the library reference, of course. I'd simply missed
it before. For anybody else looking through the archives to answer this
question later:

http://docs.python.org/api/common-structs.html

It turns out there are calling convention flags to specify class methods
and static methods. From the docs:

METH_CLASS
The method will be passed the type object as the first parameter
rather than an instance of the type. This is used to create
class methods, similar to what is created when using the
classmethod() built-in function. New in version 2.3.

METH_STATIC
The method will be passed NULL as the first parameter rather
than an instance of the type. This is used to create static
methods, similar to what is created when using the
staticmethod() built-in function. New in version 2.3.

Sorry for the noise everybody, I could've sworn I looked over that
already.

--
Craig Ringer

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Robert | last post by:
Python doesn't know the class of a method when container not direct class attribute: >>> class X: .... def f():pass .... g=f .... l= .... >>> X.g <unbound method X.f>
15
by: beliavsky | last post by:
What are the pros and cons of defining a method of a class versus defining a function that takes an instance of the class as an argument? In the example below, is it better to be able to write...
4
by: Neil Zanella | last post by:
Hello, I would like to know whether it is possible to define static class methods and data members in Python (similar to the way it can be done in C++ or Java). These do not seem to be mentioned...
50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
2
by: Krzysztof Stachlewski | last post by:
I tried to run the following piece of code: Python 2.3.4 (#53, May 25 2004, 21:17:02) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> o = object() >>> o.a...
18
by: John M. Gabriele | last post by:
I've done some C++ and Java in the past, and have recently learned a fair amount of Python. One thing I still really don't get though is the difference between class methods and instance methods. I...
2
by: Andrea Gavana | last post by:
Hello NG, this may seem a stupid (or even impossible) question, but my knowlegde of Python is quite limited. I have basically a simple graphical user interface that contains a Panel, another...
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
21
by: Mr.SpOOn | last post by:
Hi, I'm going to work on a project to represent some musical theory in Python, in an object oriented way. I have to manage many elements of music such as notes, intervals, scales, chords and so...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.