a couple of questions: pickling objects and strict types

  • Thread starter Littlefield, Tyler
  • Start date
L

Littlefield, Tyler

Hello all:
I've been using Python for a while now, but I have one larger problem.
I come from a c++ background; though it doesn't help in catching runtime
errors, being able to compile a program helps catch a lot of syntax
errors. I know about pychecker, which is somewhat useful. Do people have
other methods for handling this?

Also, I'm depickling objects. Is there a way I can force pickle to call
the object's ctor? I set up events per object, but when it just
deserializes it doesn't set all that up.
Thanks,

--
Take care,
Ty
http://tds-solutions.net
The aspen project: a barebones light-weight mud engine:
http://code.google.com/p/aspenmud
He that will not reason is a bigot; he that cannot reason is a fool; he that dares not reason is a slave.
 
J

John Gordon

In said:
Hello all:
I've been using Python for a while now, but I have one larger problem.
I come from a c++ background; though it doesn't help in catching runtime
errors, being able to compile a program helps catch a lot of syntax
errors. I know about pychecker, which is somewhat useful. Do people have
other methods for handling this?

The py_compile module can catch some obvious syntax errors, such as
incorrect indentation levels or a missing colon at the end of an if
statement.

But it won't catch other errors such as using a variable name before it's
defined. For that, you can use an external program such as pylint or
pyflakes.
 
S

Steven D'Aprano

Hello all:
I've been using Python for a while now, but I have one larger problem. I
come from a c++ background; though it doesn't help in catching runtime
errors, being able to compile a program helps catch a lot of syntax
errors. I know about pychecker, which is somewhat useful. Do people have
other methods for handling this?


Do you tend to make a lot of syntax errors?

Python also catches syntax errors at compile-time. I won't speak for
others, but I hardly ever make syntax errors: between Python's simple,
surprise-free syntax, and modern, syntax-colouring editors, I find that I
rarely make syntax errors.

Also, I'm depickling objects. Is there a way I can force pickle to call
the object's ctor? I set up events per object, but when it just
deserializes it doesn't set all that up. Thanks,

What's the object's ctor? What sort of objects are you dealing with?
 
L

Littlefield, Tyler

Do you tend to make a lot of syntax errors?
Not a -lot-, but there are things I don't catch sometimes.
Python also catches syntax errors at compile-time. I won't speak for
others, but I hardly ever make syntax errors: between Python's simple,
surprise-free syntax, and modern, syntax-colouring editors, I find that I
rarely make syntax errors.

I am blind, so colorful editors don't really work all that well for me.
What's the object's ctor? What sort of objects are you dealing with?
def __init__(self):
self.events = {}
self.components = []
self.contents = []
self.uid = uuid4().int
self.events['OnLook'] = teventlet()


Basically events don't get initialized like I'd like after I depickle
objects.

--
Take care,
Ty
http://tds-solutions.net
The aspen project: a barebones light-weight mud engine:
http://code.google.com/p/aspenmud
He that will not reason is a bigot; he that cannot reason is a fool; he that dares not reason is a slave.
 
S

Steven D'Aprano

Not a -lot-, but there are things I don't catch sometimes.

As we all do. But fortunately the Python compiler catches syntax errors.

I am blind, so colorful editors don't really work all that well for me.

Fair point.

What's the object's ctor? What sort of objects are you dealing with?
def __init__(self):
self.events = {}
self.components = []
self.contents = []
self.uid = uuid4().int
self.events['OnLook'] = teventlet()


Basically events don't get initialized like I'd like after I depickle
objects.

Did you mean "constructor" rather than C T O R ? Perhaps your voice-to-
text software (if you are using such) misheard you.

Correct, by default pickle does not call the __init__ method, it just
reconstructs the instance. Basically, it takes a snapshot of the
instance's internal state (the __dict__) and reconstructs from the
snapshot.

This is explained in the documentation here:

http://docs.python.org/2/library/pickle.html#the-pickle-protocol


You can force the __init__ method to be called in a couple of different
ways. Perhaps this is the most straight-forward. Add a __setstate__
method to your class:


def __setstate__(self, state):
self.__dict__.update(state)
self.events['OnLook'] = teventlet()
 
C

Chris Angelico

Did you mean "constructor" rather than C T O R ? Perhaps your voice-to-
text software (if you are using such) misheard you.

Side point: "ctor" is a common abbreviation for "constructor".

ChrisA
 
D

Dave Angel

Side point: "ctor" is a common abbreviation for "constructor".

ChrisA

But neither term applies to the __init__() method, which is an
initializer. The constructor is __new__()
 
M

mblume

Am Sat, 06 Apr 2013 02:37:31 +0000 schrieb Steven D'Aprano:
def __init__(self):
self.events = {}
self.components = []
self.contents = []
self.uid = uuid4().int
self.events['OnLook'] = teventlet()


Basically events don't get initialized like I'd like after I depickle
objects.


Correct, by default pickle does not call the __init__ method, it just
reconstructs the instance. Basically, it takes a snapshot of the
instance's internal state (the __dict__) and reconstructs from the
snapshot.

[...]
To the OP: Did you really mean
self.events['OnLook'] = teventlet()
as opposed to:
self.events['OnLook'] = teventlet

The first one executes teventlet and then assigns the result of the function to
self.events['OnLook']. The second one assigns the function teventlet to the dict
entry (presumably so that it will be called when the objct detects the 'OnLook'
event). Unless the teventlet() function returns itself a function, an 'OnLook'
event won't do anything useful during the remaining life time of the object, I think.

Regards
Martin
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,829
Latest member
PIXThurman

Latest Threads

Top