Don't want to serialize a variable of object

Discussion in 'Python' started by Iyer, Prasad C, Oct 18, 2005.

  1. Hi,
    I got a class which I need to serialize, except for couple of variable.
    i.e.

    import cPickle as p
    class Color:
    def __init__(self):
    print "hello world"
    self.x=10
    self.somechar="this are the characters"
    color=Color()
    f=file('poem.txt', 'w')
    p.dump(color, f)
    f.close()

    How do I serialize the object color without serializing the x and
    somechar variables?
    Is there any modifier which prevents the variable from being serialized.




    Another question:
    Is there a concept of private variables?



    regards
    prasad chandrasekaran










    --- Cancer cures smoking
    #-----Original Message-----
    #From: python-list-bounces+prasad.c.iyer=
    #[mailto:python-list-bounces+prasad.c.iyer=] On
    #Behalf Of
    #Sent: Tuesday, October 18, 2005 11:00 AM
    #To:
    #Subject: Python-list Digest, Vol 25, Issue 322
    #
    #Send Python-list mailing list submissions to
    #
    #
    #To subscribe or unsubscribe via the World Wide Web, visit
    # http://mail.python.org/mailman/listinfo/python-list
    #or, via email, send a message with subject or body 'help' to
    #
    #
    #You can reach the person managing the list at
    #
    #
    #When replying, please edit your Subject line so it is more specific
    #than "Re: Contents of Python-list digest..."

    This message contains information that may be privileged or confidential and is the property of the Capgemini Group. It is intended only for the person to whom it is addressed. If you are not the intended recipient, you are not authorized to read, print, retain, copy, disseminate, distribute, or use this message or any part thereof. If you receive this message in error, please notify the sender immediately and delete all copies of this message.
    Iyer, Prasad C, Oct 18, 2005
    #1
    1. Advertising

  2. Iyer, Prasad C

    Duncan Booth Guest

    Iyer, Prasad C wrote:

    > How do I serialize the object color without serializing the x and
    > somechar variables?
    > Is there any modifier which prevents the variable from being serialized.
    >

    See the pickle documentation. The example given in the documentation
    pickles a class while excluding one of its attributes.

    http://www.python.org/doc/2.4.2/lib/pickle-example.html

    > Another question:
    > Is there a concept of private variables?


    'private variables' can mean either of two things: hiding the variables to
    prevent accidental naming conflicts in subclasses, or security to prevent
    malicious manipulation of a class's internal state.

    Python provides the former (to a certain extent) when you give an attribute
    a name prefixed with a double underscore, but makes no attempt to provide
    the latter. Some other languages (such as C++) make no attempt to provide
    the former and a completely inadequate attempt at the latter.

    In other words: Python has a concept of private variables, but it is
    different than in some other languages, and if you google past threads you
    can find endless discussions on the pro's and con's of the Python approach.
    Duncan Booth, Oct 18, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nick

    Serialize Object in Session?

    nick, Aug 20, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    557
    George Ter-Saakov
    Aug 21, 2003
  2. Luiz Rafael Fernandes

    Serialize a object inherited Label control

    Luiz Rafael Fernandes, Apr 8, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    422
    Luiz Rafael Fernandes
    Apr 8, 2004
  3. Marcel Balcarek

    Serialize object to database example

    Marcel Balcarek, Apr 15, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    6,198
    Marcel Balcarek
    Apr 15, 2004
  4. Gordz
    Replies:
    3
    Views:
    3,923
    John Oakes
    Jun 7, 2004
  5. john harkin
    Replies:
    1
    Views:
    751
    john harkin
    Sep 23, 2004
Loading...

Share This Page