473,394 Members | 1,726 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,394 software developers and data experts.

classmethod and instance method

Hi,

I'm trying to write a method that needs to know both the class name and
the instance details

class A:

@classmethod
def meth(cls, self):
print cls
print self

a = A()
a.meth(a)

The above code seems to work as intended. Could the same effect be
achieved using a second decorator in addition to the @classmethod. I
tried

def instancemethod(self):
return self.meth(self)

@instancemethod
@classmethod
def meth(cls, self):
#do stuff

but it didn't work. Any suggestions?

Regards,
Andy

Feb 2 '06 #1
3 2108
an**************@yahoo.co.uk wrote:
Hi,

I'm trying to write a method that needs to know both the class name and
the instance details

Any suggestions?


What's the point of using a classmethod and an explicit cls argument
when you can get the class object from the instance itself?
class Test(object): def meth(self):
print self
print self.__class__
print type(self)

test = Test()
test.meth() <__main__.Test object at 0x00FA1710>
<class '__main__.Test'>
<class '__main__.Test'>

Feb 2 '06 #2
Yes that's better. Didn't know about the __class__ attribute. I
thought there must be a way to access this but couldn't find it in the
docs.

Thanks,

Andy

Feb 2 '06 #3
an**************@yahoo.co.uk wrote:
Yes that's better. Didn't know about the __class__ attribute. I
thought there must be a way to access this but couldn't find it in the
docs.

Thanks,

Andy

dir(), help() and the interactive interpreter (IDLE or CLI) are your
best friends.

Any time you wonder what an object does, or if you do something to an
object, fire the interpreter, create your object, abuse it via
dir/help/whatever, and then use dir/help/whatever on the results of
dir'ing your object.

Oh, and use new style classes too (explicitly inherit from `object` or a
built-in type), they are much more flexible and interesting than
old-style classes
class Test: # old style class pass
test = Test()
dir(test) # are completely uninteresting ['__doc__', '__module__'] class Test(object): # new style classes on the other hand pass
test = Test()
dir(test) # have 2500% more sexyness ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__',
'__hash__', '__init__', '__module__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__']

Feb 4 '06 #4

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

Similar topics

3
by: Neil Zanella | last post by:
Hello, In Python, classes are objects. But there is no way to custom print a class object. This would require some syntax such as the one commented out below: With the current "foo =...
5
by: C Gillespie | last post by:
Hi, Does anyone know of any examples on how (& where) to use staticmethods and classmethods? Thanks Colin
3
by: Giovanni Bajo | last post by:
Hello, what's the magic needed to reuse the base-class implementation of a classmethod? class A(object): @classmethod def foo(cls, a,b): # do something pass
6
by: Laszlo Zsolt Nagy | last post by:
Hello, Is it possible to tell, which instance was used to call the classmethod that is currently running? Background: I have a class called DatabaseConnection and it has a classmethod called...
6
by: ahart | last post by:
I'm pretty new to python and am trying to write a fairly small application to learn more about the language. I'm noticing some unexpected behavior in using lists in some classes to hold child...
1
by: Faheem Mitha | last post by:
Hi, Consider the following small script. My understanding of how this works is that, conceptually, class B holds a separate copy of variable x from class A. Nearly everything behaves the...
14
by: james_027 | last post by:
hi, python's staticmethod is the equivalent of java staticmethod right? with classmethod, I can call the method without the need for creating an instance right? since the difference between...
3
by: Scott SA | last post by:
Hi, I'm using the @classemethod decorator for some convenience methods and for some reason, either mental block or otherwise, can't seem to figure out howto elegantly detect if the call is from an...
0
by: Gabriel Genellina | last post by:
En Thu, 24 Apr 2008 00:27:13 -0300, Scott SA <pydev@rscorp.ab.caescribió: Then you can't use a classmethod. A class method can *only* be called on the defining class or a subclass of it, or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.