which language allows you to change an argument's value?

2007-09-30 Thread Summercool

I wonder which language allows you to change an argument's value?
like:

foo(a) {
  a = 3
}

n = 1
print n

foo(n) # passing in n, not n
print n

and now n will be 3.  I think C++ and PHP can let you do that, using
their reference (alias) mechanism.  And C, Python, and Ruby probably
won't let you do that.  What about Java and Perl?

is there any way to prevent a function from changing the argument's
value?

isn't what i pass in, the function can modify it not a desireable
behavior if i am NOT passing in the address of my argument?  For one
thing, if we use a module, and call some functions in that module, and
the module's author made some changes to his code, then we have no way
of knowing what we pass in could get changed.  Of course, if it is in
Java, Python, and Ruby, and we pass in a reference to object (not C+
+'s meaning of alias reference), so the object can get changed, but
that can be expected, vs passing in n, when n = 1.  Even when it is
Ruby, when everything is an object, passing n in when n = 1 won't ever
make n become 3.  Is there a way to prevent it from happening in the
languages that allows it?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: which language allows you to change an argument's value?

2007-09-30 Thread Summercool
On Sep 30, 4:18 am, 7stud -- [EMAIL PROTECTED] wrote:
 SpringFlowers AutumnMoon wrote:
  we have no way
  of knowing what we pass in could get changed.

 Sure you do.  You look at the function's signature.  In order to use
 someone else's library, you have to know the function's signature.  And
 the signature explicitly tells you whether the value you pass in could
 be changed.

do you mean in C++?  I tried to find signature in two C++ books and it
is not there.  Google has a few results but it looks something like
prototype.  Is signature the same as the function prototype in the .h
file?  If so, don't we usually just include ___.h and forget about
the rest.  Documentation is fine although in some situation, the
descriptions is 2 lines, and notes and warnings are 4, 5 times that,
and the users' discussing it, holding different opinion is again 2, 3
times of that length.  I think in Pascal and C, we can never have an
argument modified unless we explicitly allow it, by passing in the
pointer (address) of the argument.

also i think for string, it is a bit different because by default,
string is a pointer to char or the address of the first char in C and C
++.  So it is like passing in the address already.  it is when the
argument n is something like 1 that makes me wonder.


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The meaning of a = b in object oriented languages

2007-09-18 Thread Summercool
On Sep 17, 11:04 pm, Lloyd Linklater [EMAIL PROTECTED] wrote:
 SpringFlowers AutumnMoon wrote:
  Is that the case: if a is an object, then b = a is only copying the
  reference?

 That and it adds a counter.

 a = [foo, bar]
 b = a
 b[0] = bite me
 p a, b

 a = different
 p a, b

 ***

 In the first print, we get
 [something else, bar]
 [something else, bar]

 showing that changing b changes a, as expected.  However, if we change
 a, b is NOT changed as seen in the second print.

 different
 [something else, bar]

 That means that there is a counter inside that says to separate the two
 or b would have changed with a as a changed with b initially.

i think the line

a = different

means a is now set to a pointer to the String object with content
different.
or that a is now a reference to the String object.

and b is still a reference to the Array object.  so that's why a and b
print out different things.  they point to different objects.

i think:

whenever in Ruby, Python, and Java,

a is never an object.  a is always a reference to an object...  this
will solve a lot of puzzles when we don't understand some code
behaviors.

when a writing or a book reads a is a Hash object; a is an Array
object; or a is an Animal object it is just a short form to say that
a is a reference to that object.

b = a means whatever a is referencing to, now b is referencing it
too.

so that's why  a[1] = foobar  will change what b will display, but
a = foobar will not change what b will display.  (because a[1] =
foobar says what is  a  referencing?  go there and change its
content that has the index 1  and when b goes there to see it, it is
also changed.)


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: where are the .pyc files?

2007-09-17 Thread Summercool
On Sep 16, 6:56 pm, Steve Holden [EMAIL PROTECTED] wrote:
 Summercool wrote:

  how come a program that runs directly doesn't need to be optimized
  into bytecode first?  Or... is it that the interpreter will just run
  the program as it goes by, without ever generating a .pyc file?   So
  what if you have a program that you only update every few weeks...
  then you can ask a .pyc to be generated so that it runs faster every
  time?

 You can, if you want, compile a program manually:

http://docs.python.org/lib/module-compileall.html

 and run the resulting .pyc file if you wish. Most people don't bother,
 though. If you have a very large main program you can encapsulate it as
 a library and then call the library function from a teent-weeny main
 program that isn't worth compiling.

that's great... I just wonder why there is no command line that says

  python -c try.py

or something like that to force a generation of the byte code.



-- 
http://mail.python.org/mailman/listinfo/python-list


The meaning of a = b in object oriented languages

2007-09-17 Thread Summercool



The meaning of  a = b  in object oriented languages.


I just want to confirm that in OOP, if a is an object, then b = a is
only copying the reference.

(to make it to the most basic form:

a is 4 bytes, let's say, at memory location 0x1000 to 0x1003

b is 4 bytes, let's say, at memory location 0x2000 to 0x2003

in 0x1000 to 0x1003, it is the value 0xF000, pointing to
an object

b = a just means
copy the 4 bytes 0xF0 0x00 0x00 0x00 into 0x2000 to 0x203
so that b now points to 0xF000 which is the same object.)


so essentially, a is just a pointer to an object.

and b = a just means that put that same pointer into b.

and that's why in Python or Ruby, it is like:

 a = {a : 1, b : 2}
 b = a
 a
{'a': 1, 'b': 2}
 b
{'a': 1, 'b': 2}
 a[a] = 999
 a
{'a': 999, 'b': 2}
 b
{'a': 999, 'b': 2}

so most or all object oriented language do assignment by reference?
is there any object oriented language actually do assignment by
value?  I kind of remember in C++, if you do

Animal a, b;

a = b will actually be assignment by value.
while in Java, Python, and Ruby, there are all assignment by
reference.  (set by reference)

Is that the case: if a is an object, then b = a is only copying the
reference?

-- 
http://mail.python.org/mailman/listinfo/python-list


where are the .pyc files?

2007-09-16 Thread Summercool
so i have always heard of the .pyc files  but for some reason i
don't see them on the Windows platform...  when i have a program
called  try.py  and after running it for ages, i still don't have a
try.pyc file in my folder even if i turn the show hidden file to on.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: where are the .pyc files?

2007-09-16 Thread Summercool
On Sep 16, 10:36 am, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote:

 The `*.pyc` files are usually only created when you import a module, not
 when a module is run directly.

how come a program that runs directly doesn't need to be optimized
into bytecode first?  Or... is it that the interpreter will just run
the program as it goes by, without ever generating a .pyc file?   So
what if you have a program that you only update every few weeks...
then you can ask a .pyc to be generated so that it runs faster every
time?


-- 
http://mail.python.org/mailman/listinfo/python-list


how to join array of integers?

2007-09-15 Thread Summercool
i think in Ruby, if you have an array (or list) of integers

foo = [1, 2, 3]

you can use foo.join(,) to join them into a string 1,2,3

in Python... is the method to use  ,.join() ?  but then it must take
a list of strings... not integers...

any fast method?

-- 
http://mail.python.org/mailman/listinfo/python-list