3

I'm new to Python, so I tried writing a class to emulate string functions in C:

class CString:

  def __init__(self,str):
        self.value=[]
        self.value.extend(list(str))

def strcpy(cstring1,cstring2):
      import copy
      cstring1=copy.copy(cstring2)

def puts(cstring1):
    print ''.join(cstring1.value)

But strcpy doesn't seem to be working:

>>obj1=CString("Hello World")
>>obj2=CString("Hai World!")
>>puts(obj1)
Hello World!
>>puts(obj2)
Hai World!
>>strcpy(obj1,obj2)
>>puts(obj1)
Hello World!

Am I assigning copy.copy(cstring2) wrongly?

pupul07
  • 31
  • 2
  • 3

2 Answers2

5

The line

cstring1 = copy.copy(cstring2)

only changes the local variable called cstring1, it does not change the variable in the outer scope called obj1.

Have a look at this other stackoverflow question for more information.

Community
  • 1
  • 1
srgerg
  • 18,719
  • 4
  • 57
  • 39
  • 1
    Ah, so the reference itself is being passed by value. Thanks! – pupul07 May 28 '12 at 02:16
  • @pupul07 Not actually, objects are passed by reference. But 1. there is no way you can modify an `str` object and 2. doing an assignment is *not* modifying an object. There is no equivalent for C’s `void func(type *arg) {*arg=smth;}` and even if it was, you don’t expect `void func(type *arg) {arg=smth;}` to modify the argument pointed by `arg` instead of reassigning local variable, do you? – ZyX May 28 '12 at 07:20
  • @pupul07 Ah, I misread your comment: objects are passed by reference but reference is passed by value. Wondering in what language the code like you’ve written will modify the outer variable (without things like explicitely passing reference like by `func(&arg)` (C) or at least explicitely requesting a reference like by `sub smth(\)` (Perl, though using this feature will lead every man on #perl shouting at you once they’ve seen the code))? I find this feature rather weird and leading to hard-to-find bugs. – ZyX May 28 '12 at 07:29
1

Although you cannot modify the input parameter cstring1 in

def strcpy(cstring1,cstring2):
      import copy
      cstring1=copy.copy(cstring2)

there are other things you can do to accomplish your goal.

First, you could make strcpy part of the string class, and let that member function update its own str element.

Second, you could provide CString set and get functions. These functions could be used directly to copy one string to another, or they could be used by the member function that performs the string copy.

Python is an interesting language. It contains a lot of elements that make it seem like a regular imperative language like C or Perl, and you can construct a lot of code that looks like C. In terms of Python pureness, you'll be guided along the way not to do things in Python thinking like you're programming in C.

I suggest re-creating something like strcpy can be best accomplished by solving the problem `a la Python instead. For starters, one string can simply replace another.

Good luck.

octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131