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

Is it possible to access Methods of a class without creating object?

440 256MB
HI,

Is it possible to access the Methods of a class without creating object?


Thanks
PSB
Mar 17 '07 #1
10 3423
bartonc
6,596 Expert 4TB
HI,

Is it possible to access the Methods of a class without creating object?


Thanks
PSB
The Python Manuals says:
Class instance methods are either bound or unbound, referring to whether the method was accessed through an instance or a class, respectively. When a method is unbound, its im_self attribute will be None and if called, an explicit self object must be passed as the first argument. In this case, self must be an instance of the unbound method's class (or a subclass of that class), otherwise a TypeError is raised.

in section 2.3.10 Other Built-in Types sub 2.3.10.4 Methods (version 2.4.4)
Mar 17 '07 #2
bvdet
2,851 Expert Mod 2GB
HI,

Is it possible to access the Methods of a class without creating object?


Thanks
PSB
You can access class methods without creating a class instance.
Expand|Select|Wrap|Line Numbers
  1. class Point(object):
  2.     def __init__(self, x=0.0, y=0.0, z=0.0):
  3.         self.x = float(x)
  4.         self.y = float(y)
  5.         self.z = float(z)
  6.     def test(cls):
  7.         print 'You can access a class method without creating an instance.'
  8.     test = classmethod(test)
>>> Point.test()
You can access a class method without creating an instance.
>>>
In Python 2.4, class methods can be created with the @classmethod decorator.
Mar 17 '07 #3
bartonc
6,596 Expert 4TB
You can access class methods without creating a class instance.
Expand|Select|Wrap|Line Numbers
  1. class Point(object):
  2.     def __init__(self, x=0.0, y=0.0, z=0.0):
  3.         self.x = float(x)
  4.         self.y = float(y)
  5.         self.z = float(z)
  6.     def test(cls):
  7.         print 'You can access a class method without creating an instance.'
  8.     test = classmethod(test)
>>> Point.test()
You can access a class method without creating an instance.
>>>
In Python 2.4, class methods can be created with the @classmethod decorator.
True. But doesn't this negate the reason for having an object in the first place? Without access to self.anyThing, test() should be refactored into a different (module, perhaps) scope.
Mar 18 '07 #4
bvdet
2,851 Expert Mod 2GB
True. But doesn't this negate the reason for having an object in the first place? Without access to self.anyThing, test() should be refactored into a different (module, perhaps) scope.
I agree. I have never needed a class method without an instance.
Mar 18 '07 #5
psbasha
440 256MB
Thanks for the reply.

This helps us when we dont want to create the object of the class and make use of everywhere (helpful in creating a MathUtil module).I hope this functionality is similar to using a static functions in a class or namespace methods in C++.

Static function:
------------------------
class Sample{

public :

static void Func1(){}

};

void main(){

Sample::Func1();
}

Namespace:
------------------------
namespace Sample1{

public :

void Func1(){}

};

void main(){

Sample1::Func1();
}

-PSB
Mar 18 '07 #6
psbasha
440 256MB
If I want to access all the Methods of the classes ,so I have to define all the methods similar to " test = classmethod(test)"

class X:

def Func1():
print "Func1()"

def Func2():
print "Func2()"

.............
def Funcn():
print "Funcn()"

Func1= classmethod(Func1)
Func2= classmethod(Func2)

.....

Funcn= classmethod(Funcn)

-PSB
Mar 18 '07 #7
bvdet
2,851 Expert Mod 2GB
If I want to access all the Methods of the classes ,so I have to define all the methods similar to " test = classmethod(test)"

class X:

def Func1():
print "Func1()"

def Func2():
print "Func2()"

.............
def Funcn():
print "Funcn()"

Func1= classmethod(Func1)
Func2= classmethod(Func2)

.....

Funcn= classmethod(Funcn)

-PSB
If you use Python 2.4 or higher, use decorators:
Expand|Select|Wrap|Line Numbers
  1.     @classmethod
  2.     def Funcn():
  3.         print "Funcn()"
Why can't you use code tags?
Mar 18 '07 #8
bvdet
2,851 Expert Mod 2GB
If you use Python 2.4 or higher, use decorators:
Expand|Select|Wrap|Line Numbers
  1.     @classmethod
  2.     def Funcn():
  3.         print "Funcn()"
Why can't you use code tags?
I failed to mention that the class object is passed to a class method, so you must have an argument in the argument list. By convention, the name of this argument is cls.
Expand|Select|Wrap|Line Numbers
  1.     @classmethod
  2.     def Funcn(cls):
  3.         print "Funcn()"
You may want to use static methods instead:
Expand|Select|Wrap|Line Numbers
  1. class Point(object):
  2.     def __init__(self, x=0.0, y=0.0, z=0.0):
  3.         self.x = float(x)
  4.         self.y = float(y)
  5.         self.z = float(z)
  6.     def test(cls):
  7.         print 'You can access a class method without creating an instance.'
  8.     test = classmethod(test)
  9.     def test2():
  10.         print 'I have never used a static method.'
  11.     test2 = staticmethod(test2)
Expand|Select|Wrap|Line Numbers
  1. >>> Point.test2()
  2. I have never used a static method.
  3. >>> Point.test()
  4. You can access a class method without creating an instance.
  5. >>> 
Mar 18 '07 #9
psbasha
440 256MB
Thanks for the reply.

Here both are performing the same functionality "accessing the methods of a class without creating the object".Then why two.

Is there anything different in memory management of it?.

Thanks
PSB
Mar 18 '07 #10
bvdet
2,851 Expert Mod 2GB
Thanks for the reply.

Here both are performing the same functionality "accessing the methods of a class without creating the object".Then why two.

Is there anything different in memory management of it?.

Thanks
PSB
Not that I am aware of, except the class method receives the class object as an argument.
Mar 18 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Reid Priedhorsky | last post by:
Dear group, I'd have a class defined in one module, which descends from another class defined in a different module. I'd like the superclass to be able to access objects defined in the first...
11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
5
by: kuvpatel | last post by:
Hi I want to refer a class called LogEvent, and use one of its methods called WriteMessage without actually having to create an instance of Logevent. I have tried using the word sealed with...
6
by: Raghu | last post by:
In C#, the typeof keyword can be used to get a type of the class. This does not require object to be created first. However O am not sure how do the same thing in vb. I don't want to create the...
4
by: D | last post by:
I was reviewing these vb.net classes which deal with user logins, logouts, cookies etc etc. I noticed that in the Page_Load function of a user control the code calls into another library like so...
22
by: ypjofficial | last post by:
Is there any possibility of invoking the member functions of a class without creating an object (or even a pointer to ) of that class. eg. #include <iostream.h> class test { public: void...
2
luke14free
by: luke14free | last post by:
Greetings, I would like to create a code that can access to dll datas, but not building zend ext because i cant install anything on my host. I need that because i have to create a special system in...
1
by: tobycraftse | last post by:
in C++ you can call function / methods without creating an object of that Class? like the the following, it calls Update() + Draw() functions by Class name CInterfaceGroup + CContainer. In...
3
by: Reckoner | last post by:
would it be possible to use one of an object's methods without initializing the object? In other words, if I have: class Test: def __init__(self): print 'init' def foo(self): print 'foo'
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
0
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...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.