473,654 Members | 3,129 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

use object method without initializing object

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'

and I want to use the foo function without hitting the
initialize constructor function.

Is this possible?
Jun 27 '08 #1
3 1835
On 15 avr, 17:27, Reckoner <recko...@gmail .comwrote:
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'

and I want to use the foo function without hitting the
initialize constructor function.

Is this possible?
Yes:

In [214]: class Test:
.....: def foo(self):
.....: print 'foo'
.....:

In [215]: t = Test()

In [216]: t.foo()
foo

Jun 27 '08 #2
Reckoner wrote:
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'

and I want to use the foo function without hitting the
initialize constructor function.

Is this possible?
Hi,

Yes. It is possible and it is called "class method". That is to say, it
is a method bound to the class, and not to the class instances.
In pragmatic terms, class methods have three differences from instance
methods:
1) You have to declare a classmethod as a classmethod with the
classmethod() function, or the @classmethod decorator.
2) The first argument is not the instance but the class: to mark this
clearly, it is usually named cls, instead of self.
3) Classmethods are called with class objects, which looks like this:
ClassName.class _method_name(.. .).

In your example, this becomes:

class Test(object):
def __init__(self):
print 'init'
@classmethod
def foo(cls):
print 'foo'
Now call foo without instantiating a Test:
Test.foo()

RB
Jun 27 '08 #3
On 15 avr, 17:43, Robert Bossy <Robert.Bo...@j ouy.inra.frwrot e:
Reckoner wrote:
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'
and I want to use the foo function without hitting the
initialize constructor function.
Is this possible?

Hi,

Yes. It is possible and it is called "class method". That is to say, it
is a method bound to the class, and not to the class instances.
In pragmatic terms, class methods have three differences from instance
methods:
1) You have to declare a classmethod as a classmethod with the
classmethod() function, or the @classmethod decorator.
2) The first argument is not the instance but the class: to mark this
clearly, it is usually named cls, instead of self.
3) Classmethods are called with class objects, which looks like this:
ClassName.class _method_name(.. .).

In your example, this becomes:

class Test(object):
def __init__(self):
print 'init'
@classmethod
def foo(cls):
print 'foo'

Now call foo without instantiating a Test:
Test.foo()
To be complete, you can also define a static method that will not even
be passed the class as argument:

In [217]: class Test(object):
.....: def __init__(self):
.....: print 'init'
.....: @staticmethod
.....: def foo():
.....: print 'foo'
.....:

In [218]: Test.foo()
foo

Jun 27 '08 #4

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

Similar topics

1
1455
by: t scytale | last post by:
i am using a mutable default paramater to simulate a static method variable. i notice that - assigning to the var inside the method causes it to return to the default value at each call. - appending to the var gives the expected behaviour (the value is preserved to the next call why is this? def GetCachedVal(val=):
16
4333
by: 4Space | last post by:
I've hit something of a snag. Problem: In a DSP application we have data profiles with a varying number of points. We have a number of Discrete Fourier transforms that are optimised for a specific number of points. Currently, any client of these DFT classes must switch on the number of points in the profile and instanciate the correct DFT algorithm.
14
1811
by: Dubh | last post by:
I'm a beginner with .NET so this is probably a very simple problem that will be immediately obvious to someone hopefully. I'm using J#.NET The following contains the code caused the error Line 385: String link = "http://10.4.190.120/config/lconfig.aspx" ; Line 386: GtLink.Redirect(link); Line 387: //NLine.set_Text(Selection); STACK TRACE
12
2318
by: Ricardo Pereira | last post by:
Hello all, I have a C# class (in this example, called A) that, in its constructor, starts a thread with a method of its own. That thread will be used to continuously check for one of its object's state and generate classe's A events "Connected" and "Disconnected". It looks something like this: "
7
3297
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class ExcelService : SoapHttpClientProtocol {
1
2190
by: archana | last post by:
Hi all, I am confuse in concept of inheritance. I am having following 2 classes class base1 { public void abc() { System.Console.WriteLine("base1 abc"); }
13
2328
by: WaterWalk | last post by:
Hello. When I consult the ISO C++ standard, I notice that in paragraph 3.6.2.1, the standard states: "Objects with static storage duration shall be zero-initialized before any other initialization takes place." Does this mean all non-local objects will be zero-initialized before they are initialized by their initializers(if they have)? For example: int g_var = 3; int main() {}
5
1891
by: Andy B | last post by:
I am trying to figure out how to make an object instance available for all methods of a class. I tried to do something like this: public class test { TheObject Instance = new TheObject(); TheObject.Dictionary<string, string= new Dictionary<string, string>(); .... } The first line (TheObject instance = new TheObject();) doesn't get
23
3132
by: tonytech08 | last post by:
What I like about the C++ object model: that the data portion of the class IS the object (dereferencing an object gets you the data of a POD object). What I don't like about the C++ object model: that most OO features are not available for class object design without loss of POD-ness. So, I'm more than leaning toward "bad" because of the limitations and
0
8372
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
8285
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
8706
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
8475
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,...
0
8591
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7304
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4149
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
4293
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2709
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.