473,766 Members | 2,035 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2092
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_met hod(some_arg_li st):
pass
some_static_met hod = staticmethod(so me_static_metho d)

Then call the function as:
Demo.some_stati c_method(some_a rgs)

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

class Demo(object):
@staticmethod
def some_static_met hod(some_arg_li st):
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
3057
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
2489
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 z.modulus() or modulus(z)? Is there a difference between Python and C++ in this regard? from math import sqrt class xy: def __init__(self,x,y): self.x = x
4
8031
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 in "Learning Python" by Mark Lutz and David Ascher. It seems like they are a relatively new feature... It seems to me that any truly OO programming language should support these so I'm sure that Python is no exception, but how can these be...
50
6376
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 def foo(self, data): self.attr1=data self.attr2.append(data) The initialization of attr1 is obviously OK, all instances of Father redefine it in the method foo. But the initialization of attr2 is wrong
2
1765
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 = 5 Traceback (most recent call last): File "<stdin>", line 1, in ? AttributeError: 'object' object has no attribute 'a'
18
6956
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 guess I'll try to narrow it down to a few specific questions, but any further input offered on the subject is greatly appreciated: 1. Are all of my class's methods supposed to take 'self' as their first arg? 2. Am I then supposed to call...
2
4223
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 panel (child of the main panel) and a custom widget (child of the main panel). Basically is something like (only some small code, is in wxPython but the question is more Python-related): class MainClass(wx.Panel):
0
2834
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 magic, I wrote the following module. It is mainly useful as a light weight tool to help programmers catch mistakes at definition time (e.g., forgetting to implement a method required by the given interface). This is handy when unit tests or...
21
3946
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 on. All these elements share properties and behavior, so what I want to do is an abstract class "Note" and other subclasses, for example "NaturalNote", "FlatNote", "SharpNote" etc. The idea is not original, I read it in some papers where they...
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.