Re: [Tutor] Syntax for Simplest Way to Execute One Python Program Over1000's of Datasets

2011-06-09 Thread Alan Gauld


"B G"  wrote

I'm trying to analyze thousands of different cancer datasets and run 
the
same python program on them.  I use Windows XP, Python 2.7 and the 
IDLE

interpreter.



First thing: Don;t use IODLE as your interpreter. IDLE is a
development environment that should not be used for running
code in "production" type environments. Python is the interoreter,
use it directly.


I already have the input files in a directory and I want to
learn the syntax for the quickest way to execute the program over 
all these

datasets.


If the script take a filename as an input parameter you can likely do

python myfile.py *.*

from the OS command prompt.

Or in MacOS or Windows select all the files in Explorer and drag them 
onto

your script. I suspect most Linux desktops will allow the same.

Or you could write a second python script that traverses the files in 
your
folder and calls a function which contains your code (recall that a 
python

file can be impoerted as a module into another python file...)

Just some ideas,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Objects C++ vs Python

2011-06-09 Thread Alan Gauld

"Ashwini Oruganti"  wrote

Didn't know that!  It's interesting that  GObject is itself written 
in C,

which is a procedural laguage..


The original implementation of C++ was called cfront which was
a preprocessor which converted C++ code into vanilla C ready
for compilation. (Actually there was an earlier version but it
wasn't generally available outside Bell Labs)

OOP isn't really that mysterious, it's just a bunch of data
structures. About the only thing you really need is the ability
to create a grouped data structure and the ability to reference
a function and then include that reference within your
grouped data.

Thats why you can get OOP versions of almost any
language - even COBOL!

Alan G. 



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Syntax for Simplest Way to Execute One Python Program Over 1000's of Datasets

2011-06-09 Thread B G
hmm, thanks for the help.  So I kinda got it working, although from an
efficiency perspective it leaves a lot to be desired.

I managed to do the following:
1) Create a script that gives me a list of all the filenames in the folder:
path = "...\\Leukemia_Project"
i = 0
for (files) in os.walk(path):
print(files)
print("\n")
i += 1
2) I manually copy and paste the resulting list from the IDLE interpreter
into a .txt file
3) Open the .txt file in Excel, remove the few lines I don't need (ie single
quotes, etc)
4) Write another python script to print the result from step 3 in a new txt
file, where each filename has its own row (pretty easy to do b/c all
separated by the tab in the original txt file)
5)Put a for loop around my original python script that takes as input each
line from the result of step 4.

As it stands it is painfully awkward, but it works and has already saved me
a lot of aggravation...


On Thu, Jun 9, 2011 at 4:07 PM, Walter Prins  wrote:

>
>
> On 9 June 2011 20:49, B G  wrote:
>
>> I'm trying to analyze thousands of different cancer datasets and run the
>> same python program on them.  I use Windows XP, Python 2.7 and the IDLE
>> interpreter.  I already have the input files in a directory and I want to
>> learn the syntax for the quickest way to execute the program over all these
>> datasets.
>>
>> As an example,for the sample python program below, I don't want to have to
>> go into the python program each time and change filename and countfile.  A
>> computer could do this much quicker than I ever could.  Thanks in advance!
>>
>
> OK, so make a function of the current program logic for a single filename,
> and make the base filename a parameter to that function.  Then write another
> function (or main program) that iterates over your directory tree and calls
> the processing function for each input file.  See documentation for module
> os, specifically os.walk().  (See here:
> http://docs.python.org/library/os.html )
>
> If that doesn't get you going then post back again.
>
> Walter
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Syntax for Simplest Way to Execute One Python Program Over 1000's of Datasets

2011-06-09 Thread James Reynolds
My advice would be to stay away from generic names, like:

for item in items:
   do stuff with item

For a couple of lines its ok, but when programs get large, your program will
get confusing even to you as the author.

Sometimes, it's best just to do "for all in listx: but I think that's rare.

Usually, you know what the contents of a list are, and you know what the
list is, so for example

for assets in portfolio:
   do stuff with assets

for loans in cdo:
   do stuff with loans

for protein in gen_sequence:
   do stuff with protein.

etc.

The other piece of advice I would give you is, make it more object oriented.
This should get you away from having to rename things, as you say, and use
self referential path finding mechanisms, such as os.getcwd() (get current
working directory) and use listdir as mentioned previously.



On Thu, Jun 9, 2011 at 4:30 PM, Corey Richardson  wrote:

> On 06/09/2011 03:49 PM, B G wrote:
> > I'm trying to analyze thousands of different cancer datasets and run the
> > same python program on them.  I use Windows XP, Python 2.7 and the IDLE
> > interpreter.  I already have the input files in a directory and I want to
> > learn the syntax for the quickest way to execute the program over all
> these
> > datasets.
> >
> > As an example,for the sample python program below, I don't want to have
> to
> > go into the python program each time and change filename and countfile.
>  A
> > computer could do this much quicker than I ever could.  Thanks in
> advance!
> >
>
> I think os.listdir() would be better for you than os.walk(), as Walter
> suggested, but if you have a directory tree, walk is better. Your file
> code could be simplified a lot by using context managers, which for a
> file looks like this:
>
> with open(filename, mode) as f:
>f.write("Stuff!")
>
> f will automatically be closed and everything.
>
> Now for some code review!
>
> >
> > import string
> >
> > filename = 'draft1.txt'
> > countfile = 'draft1_output.txt'
> >
> > def add_word(counts, word):
> > if counts.has_key(word):
> > counts[word] += 1
> > else:
> > counts[word] = 1
> >
>
> See the notes on this later.
>
> > def get_word(item):
> > word = ''
> > item = item.strip(string.digits)
> > item = item.lstrip(string.punctuation)
> > item = item.rstrip(string.punctuation)
> > word = item.lower()
> > return word
>
> This whole function could be simplified to:
>
> return item.strip(string.digits + string.punctuation).lower()
>
> Note that foo.strip(bar) == foo.lstrip(bar).rstrip(bar)
>
> >
> >
> > def count_words(text):
> > text = ' '.join(text.split('--')) #replace '--' with a space
>
> How about
>
> text = text.replace('--', ' ')
>
> > items = text.split() #leaves in leading and trailing punctuation,
> >  #'--' not recognised by split() as a word
> separator
>
> Or, items = text.split('--')
>
> You can specify the split string! You should read the docs on string
> methods:
> http://docs.python.org/library/stdtypes.html#string-methods
>
> > counts = {}
> > for item in items:
> > word = get_word(item)
> > if not word == '':
>
> That should be 'if word:', which just checks if it evaluates to True.
> Since the only string that evaluate to False is '', it makes the code
> shorter and more readable.
>
> > add_word(counts, word)
> > return counts
>
> A better way would be using a DefaultDict, like so:
>
> from collections import defaultdict
> [...]
>
> def count_words(text):
>counts = defaultdict(int) # Every key starts off at 0!
>items = text.split('--')
> for item in items:
>word = get_word(item)
> if word:
>counts[word] += 1
>return counts
>
>
> Besides that things have a default value, a defaultdict is the same as
> any other dict. We pass 'int' as a parameter because defaultdict uses
> the parameter as a function for the default value. It works out because
> int() == 0.
>
> >
> > infile = open(filename, 'r')
> > text = infile.read()
> > infile.close()
>
> This could be:
>
> text = open(filename).read()
>
> When you're opening a file as 'r', the mode is optional!
>
> >
> > counts = count_words(text)
> >
> > outfile = open(countfile, 'w')
> > outfile.write("%-18s%s\n" %("Word", "Count"))
> > outfile.write("===\n")
>
> It may just be me, but I think
>
> outfile.write(('=' * 23) + '\n')
>
> looks better.
>
> >
> > counts_list = counts.items()
> > counts_list.sort()
> > for word in counts_list:
> > outfile.write("%-18s%d\n" %(word[0], word[1]))
> >
> > outfile.close
>
> Parenthesis are important! outfile.close is a method object,
> outfile.close() is a method call. Context managers make this easy,
> because you don't have to manually close things.
>
> Hope it helped,
> --
> Corey Richardson
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://m

Re: [Tutor] Syntax for Simplest Way to Execute One Python Program Over 1000's of Datasets

2011-06-09 Thread Corey Richardson
On 06/09/2011 03:49 PM, B G wrote:
> I'm trying to analyze thousands of different cancer datasets and run the
> same python program on them.  I use Windows XP, Python 2.7 and the IDLE
> interpreter.  I already have the input files in a directory and I want to
> learn the syntax for the quickest way to execute the program over all these
> datasets.
> 
> As an example,for the sample python program below, I don't want to have to
> go into the python program each time and change filename and countfile.  A
> computer could do this much quicker than I ever could.  Thanks in advance!
> 

I think os.listdir() would be better for you than os.walk(), as Walter
suggested, but if you have a directory tree, walk is better. Your file
code could be simplified a lot by using context managers, which for a
file looks like this:

with open(filename, mode) as f:
f.write("Stuff!")

f will automatically be closed and everything.

Now for some code review!

> 
> import string
> 
> filename = 'draft1.txt'
> countfile = 'draft1_output.txt'
> 
> def add_word(counts, word):
> if counts.has_key(word):
> counts[word] += 1
> else:
> counts[word] = 1
> 

See the notes on this later.

> def get_word(item):
> word = ''
> item = item.strip(string.digits)
> item = item.lstrip(string.punctuation)
> item = item.rstrip(string.punctuation)
> word = item.lower()
> return word

This whole function could be simplified to:

return item.strip(string.digits + string.punctuation).lower()

Note that foo.strip(bar) == foo.lstrip(bar).rstrip(bar)

> 
> 
> def count_words(text):
> text = ' '.join(text.split('--')) #replace '--' with a space

How about

text = text.replace('--', ' ')

> items = text.split() #leaves in leading and trailing punctuation,
>  #'--' not recognised by split() as a word separator

Or, items = text.split('--')

You can specify the split string! You should read the docs on string
methods:
http://docs.python.org/library/stdtypes.html#string-methods

> counts = {}
> for item in items:
> word = get_word(item)
> if not word == '':

That should be 'if word:', which just checks if it evaluates to True.
Since the only string that evaluate to False is '', it makes the code
shorter and more readable.

> add_word(counts, word)
> return counts

A better way would be using a DefaultDict, like so:

from collections import defaultdict
[...]

def count_words(text):
counts = defaultdict(int) # Every key starts off at 0!
items = text.split('--')
for item in items:
word = get_word(item)
if word:
counts[word] += 1
return counts


Besides that things have a default value, a defaultdict is the same as
any other dict. We pass 'int' as a parameter because defaultdict uses
the parameter as a function for the default value. It works out because
int() == 0.

> 
> infile = open(filename, 'r')
> text = infile.read()
> infile.close()

This could be:

text = open(filename).read()

When you're opening a file as 'r', the mode is optional!

> 
> counts = count_words(text)
> 
> outfile = open(countfile, 'w')
> outfile.write("%-18s%s\n" %("Word", "Count"))
> outfile.write("===\n")

It may just be me, but I think

outfile.write(('=' * 23) + '\n')

looks better.

> 
> counts_list = counts.items()
> counts_list.sort()
> for word in counts_list:
> outfile.write("%-18s%d\n" %(word[0], word[1]))
> 
> outfile.close

Parenthesis are important! outfile.close is a method object,
outfile.close() is a method call. Context managers make this easy,
because you don't have to manually close things.

Hope it helped,
-- 
Corey Richardson
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Syntax for Simplest Way to Execute One Python Program Over 1000's of Datasets

2011-06-09 Thread Walter Prins
On 9 June 2011 20:49, B G  wrote:

> I'm trying to analyze thousands of different cancer datasets and run the
> same python program on them.  I use Windows XP, Python 2.7 and the IDLE
> interpreter.  I already have the input files in a directory and I want to
> learn the syntax for the quickest way to execute the program over all these
> datasets.
>
> As an example,for the sample python program below, I don't want to have to
> go into the python program each time and change filename and countfile.  A
> computer could do this much quicker than I ever could.  Thanks in advance!
>

OK, so make a function of the current program logic for a single filename,
and make the base filename a parameter to that function.  Then write another
function (or main program) that iterates over your directory tree and calls
the processing function for each input file.  See documentation for module
os, specifically os.walk().  (See here:
http://docs.python.org/library/os.html )

If that doesn't get you going then post back again.

Walter
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Syntax for Simplest Way to Execute One Python Program Over 1000's of Datasets

2011-06-09 Thread B G
I'm trying to analyze thousands of different cancer datasets and run the
same python program on them.  I use Windows XP, Python 2.7 and the IDLE
interpreter.  I already have the input files in a directory and I want to
learn the syntax for the quickest way to execute the program over all these
datasets.

As an example,for the sample python program below, I don't want to have to
go into the python program each time and change filename and countfile.  A
computer could do this much quicker than I ever could.  Thanks in advance!

*
*


import string

filename = 'draft1.txt'
countfile = 'draft1_output.txt'

def add_word(counts, word):
if counts.has_key(word):
counts[word] += 1
else:
counts[word] = 1

def get_word(item):
word = ''
item = item.strip(string.digits)
item = item.lstrip(string.punctuation)
item = item.rstrip(string.punctuation)
word = item.lower()
return word


def count_words(text):
text = ' '.join(text.split('--')) #replace '--' with a space
items = text.split() #leaves in leading and trailing punctuation,
 #'--' not recognised by split() as a word separator
counts = {}
for item in items:
word = get_word(item)
if not word == '':
add_word(counts, word)
return counts

infile = open(filename, 'r')
text = infile.read()
infile.close()

counts = count_words(text)

outfile = open(countfile, 'w')
outfile.write("%-18s%s\n" %("Word", "Count"))
outfile.write("===\n")

counts_list = counts.items()
counts_list.sort()
for word in counts_list:
outfile.write("%-18s%d\n" %(word[0], word[1]))

outfile.close
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Objects C++ vs Python

2011-06-09 Thread Corey Richardson
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On 06/09/2011 07:03 AM, Ashwini Oruganti wrote:
> On Thu, Jun 9, 2011 at 3:32 PM, Walter Prins  wrote:
> 
>> Object Oriented code in e.g. a procedural language like C, which obviously
>> doesn't support the notion of objects explicitly in the language, although
>> then it's up to you to come up with conventions and infrastructure to
>> support the concept of object orientation in your program.
> 
> 
> 
> Didn't know that!  It's interesting that  GObject is itself written in C,
> which is a procedural laguage..
> 

(The Python interpreter you're probably using is written in C too)
Well, anything object-oriented will get executed on a processor, which
is procedural, and stored in RAM, which is essentially just a giant
array of numbers. It should come with no surprise that OO can be and is
written in a procedural language.


An object is just data+behaviour, take this snippet of C:

struct address_book_entry {
  char *name;
  char *address;
  char *extra;
};

void
printPerson(struct address_book_entry d) {
  /* d is a pointer so we could modify it */
  printf("%s, age %d\n", d.name, d.age);
  printf("Address: %s", d.address);
}


Encapsulate that, and you have yourself an object. OO isn't hard to do,
even (single!) inheritance isn't too great of a challenge. It's getting
things dynamic instead of static that's the biggest challenge.

- -- 
Corey Richardson
-BEGIN PGP SIGNATURE-
Version: GnuPG v2.0.17 (GNU/Linux)

iQEcBAEBAgAGBQJN8SAiAAoJEAFAbo/KNFvpi/oIAJfSwifBQzilopoZTS3gEggT
4ekhhW1ao8XOcmgEBSrCLL5rWwQeYDGF1USkdeSQSpUfqEb6g7ua3HuhvOimRGKs
KQx7t5jhI3AvWKE/mXslXD4nCq+vRf0q4AvGngnnAByEeQfbeZPg3huLeo6/SzXn
QLHAbKKhcHWAXwmbhx4EbTkTEgyQocVNkjYj8z9bMxRcW0IMcj+ePDsPs0IQUALO
D2T4xwjb7QwDC1OgQoGbMwTEGWup2ULxAOpMpFVwgyz2BgY7rm1PkQ2NTJuYSGIE
TYhQ4HtyevMfORjPyfvq/JJG+QYBXseucAev4PizQkCgxuA++6JF7/VHJDXJPVw=
=LXLl
-END PGP SIGNATURE-
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] lxml.html

2011-06-09 Thread Walter Prins
Hello Nitin,

On 9 June 2011 15:10, nitin chandra  wrote:

> The following is the html code in index.py
>
> html = "C Informatics"
> html += "Hospital Management & Information
> System"
> html += ''
> html += ''
> html += ''
> html += ''
> html += ''
> html += 'User Name :'
> html += 'Password   : name="paswd1">'
> html += ''
> html += 'Highest Qualifications:'
> html += ''
> html += 'Ph.D / Doctorate'
> html += 'Post Graduation'
> html += 'Post Graduation Diploma'
> html += 'Graduation'
> html += 'Diploma'
> html += 'Undergraduate'
> html += ''
> html += ''
> html += ''
> html += ''
> html += ""
>
> That's not HTML, that's python code that builds up HTML in a string. (As an
aside, it does that in a rather inefficient way.)

lxml.html is for parsing HTML, it's not meant for parsing "python that
builds HTML".  To belabor the point, lxml.html is meant for proper HTML,
nothing else.  Therefore, you should be feeding a text file (HTML file) into
LXML, not a piece of python code that builds HTML.

Where did you get that HTML?  How did it get to be part of the python code
above?

Walter
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] lxml.html

2011-06-09 Thread nitin chandra
Sorry, did not realise , on clicking "Reply", I was mailing only to
you. Thank you for pointing that out.

Duly complied.

 TypeError:
 cannot concatenate 'str' and 'NoneType' objects
>>
 I am extracting form field name / value from an html, but then it
 gives the above error and does
 not display the value of the variable 'name' entered in the respective
 form.
>>>
>>> What is the value of the variable 'name'?
>>
>> Value entered in the form is 'admin'
>
> I didn't ask what the value entered in the form is. Where does the form go?
> Where are you looking? Are you sure you're looking in the right place?

The following is the html code in index.py

 html = "C Informatics"
 html += "Hospital Management & Information System"
 html += ''
 html += ''
 html += ''
 html += ''
 html += ''
 html += 'User Name :'
 html += 'Password   :'
 html += ''
 html += 'Highest Qualifications:'
 html += ''
 html += 'Ph.D / Doctorate'
 html += 'Post Graduation'
 html += 'Post Graduation Diploma'
 html += 'Graduation'
 html += 'Diploma'
 html += 'Undergraduate'
 html += ''
 html += ''
 html += ''
 html += ''
 html += ""

>
> You're looking at a file /home/dev/wsgi-scripts/index.py and trying to
> extract XML from that file. What's inside that file?

With lxml.html i am looking to extract 'name="variable" ' from it.

Thank you

Nitin
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Objects C++ vs Python

2011-06-09 Thread Izz ad-Din Ruhulessin
Compared to Python, I do not even consider C++ an object oriënted language.

2011/6/9 Ashwini Oruganti 

> On Thu, Jun 9, 2011 at 2:18 PM, Alan Gauld wrote:
>
> C++ grew out of C so it has a lot of non OOP features. It is no
>> surprise to find therefore that its primitive types are related to
>> memory allocation and raw data rather than objects.
>>
>
>
>
>> No object is standard in OOP. It is a concept. It is the instantiated
>> encapsulation of data and function. How it is created varies between
>> language implementations.
>>
>
>
> Well, that clears it up!  I guess Python is a wider implementation of OOP,
> hence a wider use of objects. Thanks a lot.
>
>
>
>
> On Thu, Jun 9, 2011 at 3:32 PM, Walter Prins  wrote:
>
> Object Oriented code in e.g. a procedural language like C, which obviously
>> doesn't support the notion of objects explicitly in the language, although
>> then it's up to you to come up with conventions and infrastructure to
>> support the concept of object orientation in your program.
>
>
>
> Didn't know that!  It's interesting that  GObject is itself written in C,
> which is a procedural laguage..
>
>
>
>
> --
> Regards,
> Ashwini
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Objects C++ vs Python

2011-06-09 Thread Ashwini Oruganti
On Thu, Jun 9, 2011 at 2:18 PM, Alan Gauld wrote:

C++ grew out of C so it has a lot of non OOP features. It is no
> surprise to find therefore that its primitive types are related to
> memory allocation and raw data rather than objects.
>



> No object is standard in OOP. It is a concept. It is the instantiated
> encapsulation of data and function. How it is created varies between
> language implementations.



Well, that clears it up!  I guess Python is a wider implementation of OOP,
hence a wider use of objects. Thanks a lot.




On Thu, Jun 9, 2011 at 3:32 PM, Walter Prins  wrote:

> Object Oriented code in e.g. a procedural language like C, which obviously
> doesn't support the notion of objects explicitly in the language, although
> then it's up to you to come up with conventions and infrastructure to
> support the concept of object orientation in your program.



Didn't know that!  It's interesting that  GObject is itself written in C,
which is a procedural laguage..




-- 
Regards,
Ashwini
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Objects C++ vs Python

2011-06-09 Thread Walter Prins
On 9 June 2011 09:48, Alan Gauld  wrote:

>
> So does the term *Object * change its meaning when we
>> shift the context from C++ to python?? This is a little confusing,
>>
>
>
> No object is standard in OOP. It is a concept. It is the instantiated
> encapsulation of data and function. How it is created varies between
> language implementations.
>

I'd like to emphasise Alan's point, by adding/pointing out that it is
perfectly possible to write e.g. Object Oriented code in e.g. a procedural
language like C, which obviously doesn't support the notion of objects
explicitly in the language, although then it's up to you to come up with
conventions and infrastructure to support the concept of object orientation
in your program.

As examples, the X Window System was originally written in C but using
object oriented principles, also GTK+ is an object oriented cross platform
widget set written in C.  It relies on a library called GObject for
realisation  of object orientation. See e.g.
http://en.wikipedia.org/wiki/GObject

Regards

Walter
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] telnet connection question

2011-06-09 Thread Rayon
HI All, 

Is there any way that I can use python telnetlib to connect to a telnet
session. 

Send commands and get back data without closing the connection. 

I need the response to be faster and the login process is taking up too much
time. 

 

I was thinking I could use a queue to pass in data but I am not sure how i
would get it out. 

 

Regards Rayon

 

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Objects C++ vs Python

2011-06-09 Thread Alan Gauld

"Ashwini Oruganti"  wrote


I'm trying to learn Python, and know C++. I have a slight confusion
regarding the meaning of "object" in python. Here's what I've 
concluded so

far:

When we say "object" in C++, it means an instance of a class.


No, although its a common misconception.
An object in OOP, in any language is the encapsulation of data
and the operations that act on that data. It is first and foremost
a conceptuial entity. It just so happens that the normal way of
producing objects in C++ is by instantiating classes.

[But you can create objects in C++ without (explicitly) instantiating
a class, for example when passing arguments to functions/methods
temporary objects are frequently created. Also when using
templated classes there is no explicit class for a List or a 
List

we are using an implied class produced by the template at
compile/runtime.]


  int x;// x is NOT  an object, it is a *variable*


C++ grew out of C so it has a lot of non OOP features. It is no
surprise to find therefore that its primitive types are related to
memory allocation and raw data rather than objects.


while in python, from what i've understood so far,

x=5
implies that there's a memory allocation (called object) that holds 
the
value 3, and "x" is the variable (or name) that is used to refer to 
it.


There is an object created (the integer 5) that is associated with
a name(x), yes. But forget about memory allocation because,
depending on implementation, no new memory may be required.


Further, in python,  *everything *is an object, while in C++,
only*instances of a class * are called objects.


Yes, because thats how objects are created in C++, its one
of the limitations of C++ as a hybrid OOP language.


So does the term *Object * change its meaning when we
shift the context from C++ to python?? This is a little confusing,


No object is standard in OOP. It is a concept. It is the instantiated
encapsulation of data and function. How it is created varies between
language implementations.

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Objects C++ vs Python

2011-06-09 Thread Steve Willoughby

On 08-Jun-11 23:33, Ashwini Oruganti wrote:

On Thu, Jun 9, 2011 at 11:31 AM, Steve Willoughby mailto:st...@alchemy.com>> wrote:

The value 5 is an integer-class object.


But now what is "Integer-class"? Isn't integer a data type? I mean there
is no concept of "classes" in C, and yet in C, we can write


In Python, everything is an object, so integers are objects.  In C, 
integers are a fundamental data type.  C doesn't have objects at all.


Classes are data types, too, though, (the combination of a data 
representation and the set of behaviors that define what that data does 
in your program).




int x = 5;

Will "5", then be called an integer class object?


In an object-oriented language, yes.  In C, no, since it doesn't even 
have classes.



What exactly is a class now? I thought is a collection of variables and
(or) associated functions. Am I missing something here?


But in an object oriented language such as Python, we have classes and 
objects, and integers are just another class of data objects.  In this 
case, it's a very simple data structure with an associated set of 
methods to do things like add integers together, compare them with each 
other, etc.




--
Steve Willoughby / st...@alchemy.com
"A ship in harbor is safe, but that is not what ships are built for."
PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor