HI,
Is it possible to access the Methods of a class without creating object?
Thanks
PSB
10 3423
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)
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. -
class Point(object):
-
def __init__(self, x=0.0, y=0.0, z=0.0):
-
self.x = float(x)
-
self.y = float(y)
-
self.z = float(z)
-
def test(cls):
-
print 'You can access a class method without creating an instance.'
-
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.
You can access class methods without creating a class instance. -
class Point(object):
-
def __init__(self, x=0.0, y=0.0, z=0.0):
-
self.x = float(x)
-
self.y = float(y)
-
self.z = float(z)
-
def test(cls):
-
print 'You can access a class method without creating an instance.'
-
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.
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.
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
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
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: -
@classmethod
-
def Funcn():
-
print "Funcn()"
Why can't you use code tags?
bvdet 2,851
Expert Mod 2GB
If you use Python 2.4 or higher, use decorators: -
@classmethod
-
def Funcn():
-
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. -
@classmethod
-
def Funcn(cls):
-
print "Funcn()"
You may want to use static methods instead: - class Point(object):
-
def __init__(self, x=0.0, y=0.0, z=0.0):
-
self.x = float(x)
-
self.y = float(y)
-
self.z = float(z)
-
def test(cls):
-
print 'You can access a class method without creating an instance.'
-
test = classmethod(test)
-
def test2():
-
print 'I have never used a static method.'
-
test2 = staticmethod(test2)
- >>> Point.test2()
-
I have never used a static method.
-
>>> Point.test()
-
You can access a class method without creating an instance.
-
>>>
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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'
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |