473,597 Members | 2,103 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Anomaly in creating class methods

Hi,
given below is my interaction with the interpreter.... In one case, i
have created the class method using the "famous idiom"... and in the
other, i have tried to create it outside the class definition... why
isn't the latter working ? (of course, the presence of decorators is a
different issue)....
class D: .... def f(cls):
.... print cls
.... D.f=classmethod (D.f)
D.f <bound method classobj.f of <class __main__.D at 0xb7c99f5c>> D.f() Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unbound method f() must be called with D instance as first
argument (got classobj instance instead) class D: .... def f(cls):
.... print cls
.... f=classmethod(f )
.... D.f <bound method classobj.f of <class __main__.D at 0xb7c9947c>> D.f()

__main__.D

Nov 3 '05 #1
4 1828
On 3 Nov 2005 03:19:22 -0800, "venk" <ve************ ****@gmail.com> wrote:
Hi,
given below is my interaction with the interpreter.... In one case, i
have created the class method using the "famous idiom"... and in the
other, i have tried to create it outside the class definition... why
isn't the latter working ? (of course, the presence of decorators is a
different issue)....
class D:... def f(cls):
... print cls
... D.f=classmethod (D.f)
D.f<bound method classobj.f of <class __main__.D at 0xb7c99f5c>> D.f()Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unbound method f() must be called with D instance as first
argument (got classobj instance instead) class D:... def f(cls):
... print cls
... f=classmethod(f )
... D.f<bound method classobj.f of <class __main__.D at 0xb7c9947c>> D.f()__main__.D


I think you are on very thin ice using classmethod for old-style classes.
In any case, you are not providing the same argument to classmethod
in the two examples above. I'm not sure what classmethod tries to do with
an unbound method, but it's not normal usage even in newstyle classes.
It probably just saves the callable argument as an attribute of the
classmethod instance, so the problem doesn't show until the descriptor
machinery comes into play (on attribute access and calling the bound callable).

def showcmarg(f): print 'cmarg=%r'%f; return classmethod(f) ... class D: ... def f(cls): print cls
... D.f=showcmarg(D .f) cmarg=<unbound method D.f>

Note that that was an unbound method as the arg, that gets stored in the descriptor
D.f <bound method classobj.f of <class __main__.D at 0x02EE9A1C>> D.f() Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unbound method f() must be called with D instance as first argument (got classobj
tance instead)

That's just the callable (D unbound method) you gave it complaining that the descriptor is
passing the intended cls D (a classobj) instead of an instance of D to the unbound method.
class D: ... def f(cls): print cls
... f = showcmarg(f)
...
cmarg=<function f at 0x02EEAE2C>

Note that this time that was a _function_ arg, because it was picked up as a local
binding during the execution of the body of the class definition, where classmethod
was called. The function will have no demands except to match its signature when the
classmethod descriptor instance calls it with first arg of D.
D.f <bound method classobj.f of <class __main__.D at 0x02EE9A7C>> D.f() __main__.D

You could extract the normal (function) classmethod arg from the outside though:
class D: ... def f(cls): print cls
... D.f=showcmarg(D .f.im_func) cmarg=<function f at 0x02EEAD84> D.f <bound method classobj.f of <class __main__.D at 0x02EE9A4C>> D.f()

__main__.D

Suggest migrating to new style classes consistently ;-)

Regards,
Bengt Richter
Nov 3 '05 #2
venk <ve************ ****@gmail.com> wrote:
Hi,
given below is my interaction with the interpreter.... In one case, i
have created the class method using the "famous idiom"... and in the
other, i have tried to create it outside the class definition... why
isn't the latter working ? (of course, the presence of decorators is a
different issue)....


When you access D.f, you get an unbound method; if you want the
underlying function instead, use D.f.im_func. When you access f within
the class's body, you do get the underlying function -- that's all of
the difference that's biting you...
Alex
Nov 4 '05 #3
Cool,
i got it now... accessing thru attribute reference always
returns a bound or unbound method... so, D.f is an unbound method
whereas i want the "function" of the unbound method... ok,.... this
example and the nice explanations definitively thought me about
function, bound method (for which the function is just an attribute
accessed by im_func) and unbound method...
Also, calling classmethod on an unbound method instead of a function
is a strict no... no...
ok... nice concept...

But, i am at loss when i come to clearly understanding the difference
between new-style classes and classic classes.... (or should i post it
as new topic?).....

Nov 4 '05 #4
venk <ve************ ****@gmail.com> wrote:
Cool,
i got it now... accessing thru attribute reference always
returns a bound or unbound method... so, D.f is an unbound method
Right. Specifically, accessing through a (newstyle) class or instance
always calls the __get__ method of a descriptor [[oldstyle classes and
instances are harder to pin down -- avoid them in new code!!!]]; every
function is a descriptor, so...
whereas i want the "function" of the unbound method... ok,.... this
example and the nice explanations definitively thought me about
function, bound method (for which the function is just an attribute
accessed by im_func) and unbound method...
Great!
Also, calling classmethod on an unbound method instead of a function
is a strict no... no...
ok... nice concept...
Glad you like it.
But, i am at loss when i come to clearly understanding the difference
between new-style classes and classic classes.... (or should i post it
as new topic?).....


"Classic" classes (old-style) exist (and are the default) only for
LEGACY purposes; they exhibit a few problematic quirks. New-style
classes are more regular, fully predictable, easy to explain and to
understand. In new code, use new-style classes only (make sure every
class includes 'object' among its ancestors -- that's the simplest way).

And, sure, feel free to open another thread if you desire more
information about the several small differences between the new-style
object model and the old-style, legacy one.
Alex
Nov 4 '05 #5

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

Similar topics

8
3312
by: Piro | last post by:
I have a class that I want to make accessible to a web service. This class does some work in its constructor method and sets some class variables in its various methods. The problem I am having is creating an instance of this class when it is called via SOAP. I don't seem to have access to the constructor method or any class variables... is this by design? Must all methods be static? Here is my sample code: This is a very dumbed...
15
6737
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use javascript or vbscript it works. I will appreciate any help. I do the following (C#):
3
1853
by: Ken Varn | last post by:
I am just starting the process of creating ASP.NET server controls. I have created controls for .NET applications, but have just started with ASP.NET. I am a little confused about some areas that I am hoping someone can help clear up. 1. What is the difference between initializing a control in the constructor, vs the OnInit(), vs the CreateChildControls() methods? 2. The control that I created contains an Items collection attribute...
6
1651
by: Don Wash | last post by:
Hi All! I'm developing ASP.NET pages using VB.NET language. My background is VB6 and ASP3. Right now, I'm evaluating strategies on creating reusable and common functions and methods for ASP.NET. In ASP 3, it was nothing more than functions and methods coded in #Include Files. Now ASP.NET offers features such as Page Inheritance and full software development support. So I'm just wondering, what should I do if I have 5 functions, that...
12
3149
by: Mats Lycken | last post by:
Hi, I'm creating a CMS that I would like to be plug-in based with different plugins handling different kinds of content. What I really want is to be able to load/unload plugins on the fly without restarting the application. What I did was to create an AppDomain that loaded the plugins and everything was great, until I tried to pass something else that strings between the domains...
26
5348
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is because I am not always able to control/create all the different constructors the base class has. My problem can be described in code as follows ... /* This is the base class with a whole heap of constructors/functionality*/ public class Animal
17
46498
Motoma
by: Motoma | last post by:
This article is cross posted from my personal blog. You can find the original article, in all its splendor, at http://motomastyle.com/creating-a-mysql-data-abstraction-layer-in-php/. Introduction: The goal of this tutorial is to design a Data Abstraction Layer (DAL) in PHP, that will allow us to ignore the intricacies of MySQL and focus our attention on our Application Layer and Business Logic. Hopefully, by the end of this guide, you will...
1
2790
by: mai | last post by:
Hi everyone, i'm trying to exhibit FIFO anomaly(page replacement algorithm),, I searched over 2000 random strings but i couldnt find any anomaly,, am i I doing it right?,, Please help,,,The following is the code,, #include <iostream> #include <string> #include <stdio.h> #include <stdlib.h> #include <ctime // For time()
2
2652
by: slizorn | last post by:
hi guys, i need to make a tree traversal algorithm that would help me search the tree.. creating a method to search a tree to find the position of node and to return its pointer value basically i need to read in a text file... shown below H H,E,L E,B,F B,A,C A,null,null
0
7962
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
7884
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8267
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...
0
8380
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8024
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
5844
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
3880
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3921
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2394
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

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.