1

I am curious about this: what actually happens to the python objects once that you create a class that contains each one of these functions?

Looking at some example, I see that either the bound, static or class function is in fact creating a class object, which is the one that contains all 3 function.

Is this always true, no matter which function I call? and the parent object class (object in this case, but can be anything I think) is always called, since the constructor in my class is invoking it implicitly?

class myclass(object):
    a=1
    b=True

    def myfunct(self, b)
        return (self.a + b)

    @staticmethod
    def staticfunct(b):
        print b

    @classmethod
    classfunct(cls, b):
        cls.a=b

Since it was not clear: what is the lifecycle for this object class, when I use it as following?

from mymodule import myclass

class1 = myclass()
class1.staticfunct(4)
class1.classfunct(3)
class1.myfunct

In the case of static, myclass object get allocated, and then the function is run, but class and bound method are not generated?

In the case of class funciton, it is the same as above?

in the case of the bound function, everything in the class is allocated?

7
  • 1
    The syntax is invalid: one doesn't specify "self" when predefining fields in classes. Commented Jul 31, 2015 at 22:28
  • Sorry, just typing fast. Commented Jul 31, 2015 at 22:31
  • Still invalid. Try to run your code before putting it on SO. What exactly is your question here? What is creating what? Some of these terms are invalid, and the ambiguous grammar doesn't provide enough context clues to resolve them. Is there unexpected behavior in your example class? If so, provide code presenting that unexpected behavior. All of these values are functions. Where is the confusion? Commented Jul 31, 2015 at 22:46
  • There is no code example: the question is "what is the lifecycle of a class, when you call one of the 3 functions in it, where each function is declared as either static, class or bound". I am not trying to solve any problem, there is nothing broken (yet), but it may be if I use this incorrectly, so I am trying to learn how it works. Commented Jul 31, 2015 at 22:51
  • 1
    Then I would strongly recommend slowing down. Commented Jul 31, 2015 at 23:01

3 Answers 3

1

The class statement creates the class. That is an object which has all three functions, but the first (myfunct) is unbound and cannot be called without an instance object of this class.

The instances of this class (in case you create them) will have bound versions of this function and references to the static and the class functions.

So, both the class and the instances have all three functions.

None of these functions create a class object, though. That is done by the class statement. (To be precise: When the interpreter completes the class creation, i. e. the class does not yet exist when the functions inside it are created; mind boggling, but seldom necessary to know.)

If you do not override the __init__() function, it will be inherited and called for each created instance, yes.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the reply; so no matter if I use a static, class or bound method; the class itself is allocated in memory. But with static, it won't create the bound method in memory, nor the class parameters, right? I did expand the question to include the usage of such class, to make the question clearer
I didn't say that "with static it will not create a bound method". I said the class object has an unbound function. What do you mean by "class parameters"? About the edit of your question: class1 will be an instance of myclass. Whatever you call on that, it won't create anything.
I didn't say that you said that; I was checking if what I see is in fact what is really happening. When I call myclass(); the object is created in memory; so I in fact created and allocated an instance of myclass (unless python is going crazy, printing out an address that is a lie)
Yeah, that's right. Creating an instance will create it in memory. It will have references to the three functions which have been created earlier; one reference will be slightly modified (it will be a bound method instead of an unbound one which is present in the class object). The class object, however, is created when the class statement is finished, i. e. way before you create an instance using the myclass() call.
THANKS!!! I am glad that I was able to get to the point that I am aiming at, Could you please point me out to any resource that teach how to actually follow the life cycle of objects? I would like to see the memory allocation, address and such, when I use classes and functions inside classes; to better optimize my system. I know that high languages like Python were meant to NOT show the inner workings, but I need to know these details to accomplish my goal. Thanks again Alfe.
|
1

Since it was not clear: what is the lifecycle for this object class, when I use it as following?

from mymodule import myclass

This will create the class, and code for all functions. They will be classmethod, staticmethod, and method (which you can see by using type() on them)

class1 = myclass()

This will create an instance of the class, which has a dictionary and a lot of other stuff. It doesn't do anything to your methods though.

class1.staticfunct(4)

This calls your staticfunct.

class1.classfunct(3)

This calls you classfunct

class1.myfunct

This will create a new object that is a bound myfunct method of class1. It is often useful to bind this to a variable if you are going to be calling it over and over. But this bound method has normal lifetime.

Here is an example you might find illustrative:

>>> class foo(object):
...     def bar(self):
...         pass
... 
>>> x = foo()
>>> x.bar is x.bar
False

Every time you access x.bar, it creates a new bound method object.

And another example showing class methods:

>>> class foo(object):
...  @classmethod
...  def bar():
...     pass
... 
>>> foo.bar
<bound method type.bar of <class '__main__.foo'>>

Comments

0

Your class myclass actually has four methods that are important: the three you explicitly coded and the constructor, __init__ which is inherited from object. Only the constructor creates a new instance. So in your code one instance is created, which you have named class1 (a poor choice of name).

myfunctcreates a new integer by adding class1.a to 4. The lifecycle of class1 is not affected, nor are variables class1.a, class1.b, myclass.a or myclass.b.

staticfunct just prints something, and the attributes of myclass and class1 are irrelevant.

classfunct modifies the variable myclass.a. It has no effect on the lifecycle or state of class1.

The variable myclass.b is never used or accessed at all; the variables named b in the individual functions refer to the values passed in the function's arguments.

Additional info added based on the OP's comments:

Except for the basic data types (int, chars, floats, etc) everything in Python is an object. That includes the class itself (a class object), every method (a method object) and every instance you create. Once created each object remains alive until every reference to it disappears; then it is garbage-collected.

So in your example, when the interpreter reaches the end of the class statement body an object named "myclass" exists, and additional objects exist for each of its members (myclass.a, myclass.b, myclass.myfunct, myclass.staticfunct etc.) There is also some overhead for each object; most objects have a member named __dict__ and a few others. When you instantiate an instance of myclass, named "class1", another new object is created. But there are no new method objects created, and no instance variables since you don't have any of those. class1.a is a pseudonym for myclass.a and similarly for the methods.

If you want to get rid of an object, i.e., have it garbage-collected, you need to eliminate all references to it. In the case of global variables you can use the "del" statement for this purpose:

A = myclass() del A

Will create a new instance and immediately delete it, releasing its resources for garbage collection. Of course you then cannot subsequently use the object, for example print(A) will now give you an exception.

2 Comments

I must have problems to explain what I am talking about; class1() allocate memory for that object of type "class". And this is a given to this point. The class stop to exist at one point in time, when it is not in use (I am new to python, I do not have memory allocation and pointer release here, so bear with me). this memory space allocated for the class instance, is including also the functions inside the class itself? Does the class object have an address where it is defined, and allocate memory based on the function types that has inside it?
This is basically what I am trying to figure out...If I have to run on a device that has 256kb of ram memory, I need to know how to maximize each object that I use. If a class generate the same footprint in memory, independently if it has static, class or bound methods, I will have to find a different way to run on this device. Thanks for the reply btw

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.