FAQ
I am new to python and am wondering. When I create a class, with 'def'
functions and if this class is instantiated say 50 times. Does this mean
that all the 'def' functions code within the class is duplicated for each
instance?

Can someone give me a short and simple answer as to what happens in python?



Thanks



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080917/a3afa8dc/attachment.htm>

Search Discussions

  • Gary Herron at Sep 18, 2008 at 7:11 am

    Karl Kobata wrote:
    I am new to python and am wondering. When I create a class, with ?def?
    functions and if this class is instantiated say 50 times. Does this
    mean that all the ?def? functions code within the class is duplicated
    for each instance?

    Can someone give me a short and simple answer as to what happens in
    python?

    Thanks
    No code is duplicated. 50 "objects" are created. Each object has its own
    copy of the data attributes, and a reference to the (one and only) class
    object where the method attributes are located.

    That's a short answer. Perhaps too short?

    Gary Herron
    ------------------------------------------------------------------------

    --
    http://mail.python.org/mailman/listinfo/python-list
  • Terry Reedy at Sep 18, 2008 at 7:55 am

    Karl Kobata wrote:
    I am new to python and am wondering. When I create a class, with ?def?
    functions and if this class is instantiated say 50 times. Does this
    mean that all the ?def? functions code within the class is duplicated
    for each instance?

    Can someone give me a short and simple answer as to what happens in python?
    To expand slightly on Gary's answer. Dotted names -- ob.attr -- are
    typically (but not always) resolved as follows: does ob have attribute
    attr? If yes, get the corresponding object. If no, does type(ob) have
    attribute attr? If yes, get the corresponding object. In no, look in
    the baseclasses of type(ob) in some order (details not important here).

    So, instance methods are class attributes and instance.method will
    resolve to the method attribute of type(instance) -- assuming that the
    instance does not have an attribute of the same name. Lookup for
    special methods (with reserved __xxx__ names) may go directly to the
    class and never look at the instance attributes.
  • Karl Kobata at Sep 18, 2008 at 2:33 pm
    Gary,



    No the answer is not too short, thank you for your reply, I am learning
    rapidly.



    Terry,



    The expanded answer is also useful. Now I am getting a better insight on
    how python resolves object attributes.

    This also gives me more insight on the difference between import <libname>
    vs from <libname> from *.



    Thanks

    karl

    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20080918/56d3a253/attachment.htm>

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedSep 18, '08 at 6:43a
activeSep 18, '08 at 2:33p
posts4
users3
websitepython.org

People

Translate

site design / logo © 2023 Grokbase