HI,
Is it possible to access the Methods of a class without creating object?
Thanks
PSB
10 3444 bartonc 6,596
Recognized Expert Expert
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
Recognized Expert Moderator Specialist
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.
bartonc 6,596
Recognized Expert Expert
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
Recognized Expert Moderator Specialist
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(tes t)"
class X:
def Func1():
print "Func1()"
def Func2():
print "Func2()"
.............
def Funcn():
print "Funcn()"
Func1= classmethod(Fun c1)
Func2= classmethod(Fun c2)
.....
Funcn= classmethod(Fun cn)
-PSB
bvdet 2,851
Recognized Expert Moderator Specialist
If I want to access all the Methods of the classes ,so I have to define all the methods similar to " test = classmethod(tes t)"
class X:
def Func1():
print "Func1()"
def Func2():
print "Func2()"
.............
def Funcn():
print "Funcn()"
Func1= classmethod(Fun c1)
Func2= classmethod(Fun c2)
.....
Funcn= classmethod(Fun cn)
-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
Recognized Expert Moderator Specialist
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
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 module (given an instance of the first
class) without importing it. Example of what I'm looking for:
<<<file spam.py>>>
class Spam(object):
|
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 variables".
However, this doesn't seem entirely correct. It also doesn't mention
whether static member functions can access protected and private
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 the class and this works but I would
also like to know of other ways to do this.
Also are there any performance implacations of using sealed?
|
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 object just to get its type. I
would appreciate if any one can can show me how to do this in vb.net.
Thanks.
Raghu/..
|
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
Call AnotherClassLibrary.AnotherClass.UserLogin(userId)
I do not understand how this code can call this function without first
creating an...
| |
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 fun()
{
|
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 c++ ( maybe in python :) ) but until i cant access to dlls or pyd(python compiled data) i wont be able to do anything.
To help you helping me (!:D!)...
|
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 Java, one can only call method by Class name if it is static
method.
In C++, you can call any function without creating an Object like -
|
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: 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...
|
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. ...
| |
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...
|
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...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
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...
|
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...
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |