I'm a complete python n00b writing my first program (or attempting to,
anyway). I'm trying to make the transition from Java, so if you could
help me, it would be greatly appreciated. Here's the code I'm stuck on
(It's very basic):
class DemoClass:
def __init__(self):
self.title = ["Hello", "Goodbye"]
def WriteToFile(self, path = "test.txt"):
fw = file(path, "w")
pickle.dump(self.title, fw)
fw.close()
if __name__=='__main__':
x = DemoClass
x.WriteToFile
It doesn't do any file I/O at all (that I see). I hope my syntax is
alright. If I just call WriteToFile, shouldn't it perform with the
default path? It gives me no errors and pretends to execute just fine. 9 1309
On Mar 15, 11:13 am, "tonyr1988" <tonyr1...@gmail.comwrote:
if __name__=='__main__':
x = DemoClass
x.WriteToFile
You meant to create a DemoClass instance object, but instead, you
obtained a reference to the class object. You want 'x = DemoClass()'
instead.
You meant to call the WriteToFile method, but instead, you obtained a
reference to the method object. You want 'x.WriteToFile()' instead.
In <11**********************@d57g2000hsg.googlegroups .com>, tonyr1988
wrote:
if __name__=='__main__':
x = DemoClass
x.WriteToFile
In Python classes, functions and methods are first class objects. You
bind the `DemoClass` class object to the name `x`, you are *not* creating
an instance of `DemoClass`.
Then you access the attribute `WriteToFile` of the `DemoClass` class
object. But you don't do anything with it.
In [39]: class DemoClass(object): pass
....:
In [40]: x = DemoClass
In [41]: x
Out[41]: <class '__main__.DemoClass'>
In [42]: y = DemoClass()
In [43]: y
Out[43]: <__main__.DemoClass object at 0xb5a3fd4c>
In [44]: x()
Out[44]: <__main__.DemoClass object at 0xb5a3fc2c>
You have to call the class object and the method to see any effects.
Ciao,
Marc 'BlackJack' Rintsch
tonyr1988 wrote:
I'm a complete python n00b writing my first program (or attempting to,
anyway). I'm trying to make the transition from Java, so if you could
help me, it would be greatly appreciated. Here's the code I'm stuck on
(It's very basic):
class DemoClass:
def __init__(self):
self.title = ["Hello", "Goodbye"]
def WriteToFile(self, path = "test.txt"):
fw = file(path, "w")
pickle.dump(self.title, fw)
fw.close()
if __name__=='__main__':
x = DemoClass
x.WriteToFile
It doesn't do any file I/O at all (that I see). I hope my syntax is
alright. If I just call WriteToFile, shouldn't it perform with the
default path? It gives me no errors and pretends to execute just fine.
Just a couple of "issues" that can be fixed as follows:
import pickle
class DemoClass:
def __init__(self):
self.title = ["Hello", "Goodbye"]
def WriteToFile(self, path):
fw = file(path, "w")
pickle.dump(self.title, fw)
fw.close()
if __name__=='__main__':
path='\\test.txt'
x = DemoClass()
x.WriteToFile(path)
Notes:
1) You have to call (follow by parenthesis) DemoClass() to get an instance.
What you got was a pointer (x) to the DemoClass not an instance of
DemoClass.
2) Same for WriteToFile()
3) Probably best to move the path to main and always pass it into
WriteToFile.
-Larry
On 15 Mar 2007 08:13:53 -0700, tonyr1988 <to*******@gmail.comwrote:
if __name__=='__main__':
x = DemoClass
x.WriteToFile
Here, you're binding the Class DemoClass to the name x. What you
probably meant to do is create a new instance of DemoClass, and bind
that to name x, like this:
if __name__=='__main__':
x = DemoClass()
x.WriteToFile
When I make that change, your code appears to work fine.
--
Jerry
tonyr1988 wrote:
I'm a complete python n00b writing my first program (or attempting to,
anyway). I'm trying to make the transition from Java, so if you could
help me, it would be greatly appreciated. Here's the code I'm stuck on
(It's very basic):
class DemoClass:
def __init__(self):
self.title = ["Hello", "Goodbye"]
def WriteToFile(self, path = "test.txt"):
fw = file(path, "w")
pickle.dump(self.title, fw)
fw.close()
if __name__=='__main__':
x = DemoClass
x.WriteToFile
It doesn't do any file I/O at all (that I see). I hope my syntax is
alright. If I just call WriteToFile, shouldn't it perform with the
default path? It gives me no errors and pretends to execute just fine.
Several people have pointed out the problem, but when you get that
fixed, I see another bit of trouble. The pickle format is a binary
format (be default), but you don't open the file in binary mode. On
Unix the distinction is (wisely) irrelevant, but on Windows you should
open the file with a mode of "wb" not just "w".
Hi,
You should write your last two lines as ...
x = DemoClass()
x.WriteToFile()
Don't miss paranthesis again... :)
Maybe there are still some mistakes too. Does dump method writes list's
elements?
Sönmez
tonyr1988 wrote:
I'm a complete python n00b writing my first program (or attempting to,
anyway). I'm trying to make the transition from Java, so if you could
help me, it would be greatly appreciated. Here's the code I'm stuck on
(It's very basic):
class DemoClass:
def __init__(self):
self.title = ["Hello", "Goodbye"]
def WriteToFile(self, path = "test.txt"):
fw = file(path, "w")
pickle.dump(self.title, fw)
fw.close()
if __name__=='__main__':
x = DemoClass
x.WriteToFile
It doesn't do any file I/O at all (that I see). I hope my syntax is
alright. If I just call WriteToFile, shouldn't it perform with the
default path? It gives me no errors and pretends to execute just fine.
On Mar 15, 10:38 am, Gary Herron <gher...@islandtraining.comwrote:
tonyr1988 wrote:
I'm a complete python n00b writing my first program (or attempting to,
anyway). I'm trying to make the transition from Java, so if you could
help me, it would be greatly appreciated. Here's the code I'm stuck on
(It's very basic):
class DemoClass:
def __init__(self):
self.title = ["Hello", "Goodbye"]
def WriteToFile(self, path = "test.txt"):
fw = file(path, "w")
pickle.dump(self.title, fw)
fw.close()
if __name__=='__main__':
x = DemoClass
x.WriteToFile
It doesn't do any file I/O at all (that I see). I hope my syntax is
alright. If I just call WriteToFile, shouldn't it perform with the
default path? It gives me no errors and pretends to execute just fine.
Several people have pointed out the problem, but when you get that
fixed, I see another bit of trouble. The pickle format is a binary
format (be default), but you don't open the file in binary mode. On
Unix the distinction is (wisely) irrelevant, but on Windows you should
open the file with a mode of "wb" not just "w".
Thanks guys for all the help. Sure enough, it was the parenthesis.
Most of my problems seem to be from under-simplifying (using
semicolons, brackets, etc) or, rarely, over-simplifying (forgetting
parenthesis). The biggest thing that was messing me up was the
mandatory "self" input. For some reason I was thinking that, if I had
parenthesis, I would have to define it. Fixing that works perfectly.
Also, about the binary format for opening files. The program that I'm
working on now is completely Linux-based - it's impossible for it to
work on any other OS. Should I still open with "wb" or not? Either
way, thanks for that tip!
One more (completely irrelevant) question. I don't quite understand
the double-underscore functions / methods / etc, such as __name__,
__main__, __init__. Is there a reason for the double-underscore? Does
it make anything special?
Again, thanks for the help...I'm probably going to ask a lot more of
it before too long. :)
Gary Herron <gh*****@islandtraining.comwrote:
...
fixed, I see another bit of trouble. The pickle format is a binary
format (be default), but you don't open the file in binary mode. On
Alas, wish it were:-(.
Unfortunately, snipping the right snippet from help(pickle) ...:
"""
| The optional protocol argument tells the pickler to use the
| given protocol; supported protocols are 0, 1, 2. The
default
| protocol is 0, to be backwards compatible. (Protocol 0 is
the
| only protocol that can be written to a file opened in text
| mode and read back successfully. When using a protocol
higher
| than 0, make sure the file is opened in binary mode, both
when
| pickling and unpickling.)
|
| Protocol 1 is more efficient than protocol 0; protocol 2 is
| more efficient than protocol 1.
"""
So, by default, you're using the most inefficient protocol... but, you
can write it out to a file opened in text mode and read it back.
Alex
I'm trying to make the transition from Java
The biggest thing that was messing me up was the
mandatory "self" input. For some reason I was thinking
that, if I had parenthesis, I would have to define it.
I think things are pretty similar in Java. Java does the same thing
except 'self' is invisible in Java, and in Java 'self' is called
'this'. For instance, in Java you can write:
int num;
void setNum(int num)
{
this->num = num
}
Where did 'this' come from? In Java, methods are passed the invisible
'this' argument, which you can then access inside the method. It
looks like Python just "uncloaks" Java's 'this'. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Simon Burton |
last post by:
Hi,
I am pickling big graphs of data and running into this problem:
File "/usr/lib/python2.2/pickle.py", line 225, in save
f(self, object)
File "/usr/lib/python2.2/pickle.py", line 414, in...
|
by: Michael Hohn |
last post by:
Hi,
under python 2.2, the pickle/unpickle sequence incorrectly restores
a larger data structure I have.
Under Python 2.3, these structures now give an explicit exception from...
|
by: Mike P. |
last post by:
Hi all,
I'm working on a simulation (can be considered a game) in Python where I
want to be able to dump the simulation state to a file and be able to load
it up later. I have used the standard...
|
by: crystalattice |
last post by:
I'm creating an RPG for experience and practice. I've finished a
character creation module and I'm trying to figure out how to get the
file I/O to work.
I've read through the python newsgroup...
|
by: Chris |
last post by:
Why can pickle serialize references to functions, but not methods?
Pickling a function serializes the function name, but pickling a
staticmethod, classmethod, or instancemethod generates an...
| |
by: Victor Lin |
last post by:
Hi,
I encounter a problem with pickle.
I download a html from:
...
|
by: fizilla |
last post by:
Hello all!
I have the following weird problem and since I am new to Python I somehow cannot figure out an elegant solution. The problem reduces to the following question:
How to pickle a...
|
by: Nagu |
last post by:
I am trying to save a dictionary of size 65000X50 to a local file and
I get the memory error problem.
How do I go about resolving this? Is there way to partition the pickle
object and combine...
|
by: Nagu |
last post by:
I am trying to save a dictionary of size 65000X50 to a local file and
I get the memory error problem.
How do I go about resolving this? Is there way to partition the pickle
object and combine...
|
by: IceMan85 |
last post by:
Hi to all, I have spent the whole morning trying, with no success to pickle an object that I have created.
The error that I get is : Can't pickle 'SRE_Match' object: <_sre.SRE_Match object at...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
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...
|
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...
|
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,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |