Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #50280

Re: Recursive class | can you modify self directly?

From Dave Angel <davea@davea.name>
Subject Re: Recursive class | can you modify self directly?
Date 2013-07-09 18:30 -0400
References <61751485-a298-403e-8b44-be7cf2504f0e@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.4477.1373409051.3114.python-list@python.org> (permalink)

Show all headers | View raw


On 07/09/2013 06:01 PM, Russel Walker wrote:
> Sorry for the vague title. Probably best to just show you the code that explains it better.
>
> This is a simplified example of what I want to do:
>
>
> # THIS DOESN'T WORK
> from random import choice
>
> class Expr(object):
>      """
>      Expr(expr, op, val) -> an expression object.
>      """
>
>      def __init__(self, expr, op='', val=''):
>          self.expr = expr # can be another instance of Expression.
>          self.op = op
>          self.val = val
>
>      def __str__(self):
>          return ("%s %s %s" % (self.expr, self.op, self.val)).strip()
>
>      def expand(self):
>          self = Expr(self, choice('+-*/'), choice('12345'))
>
>
> Then I tried messing around with Expr.__new__() and Expr.__init__() but that didn't work.
>
> The only way I've got it to work is by doing the expanding part outside of the object, by a method of some other class. But I want the Expr class to be responsible for itself. How can I do this and why doesn't the above work?
>

That line in method expand() indicates that you have a fundamental 
misunderstanding of the way that names and objects interact in Python. 
Names are bound to objects by the assignment operator, but the object 
itself is not changed.  If you want to change the object that self is 
bound to, then change it by mutating it.

You don't change an existing object by binding a name to a new object, 
whether of the same or different type.  The original object remains 
unchanged.  The fact that the name is 'self' is totally irrelevant.

a = 45
b = a
a = 12

This does NOT change b.



No idea what you really wanted, but perhaps it's something like:
       def expand(self):
            self.op = choice("+-*/")
            self.val = choice("12345")

No idea what expr is supposed to represent, so I didn't try to 
manipulate it here.




-- 
DaveA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Recursive class | can you modify self directly? Russel Walker <russ.pobox@gmail.com> - 2013-07-09 15:01 -0700
  Re: Recursive class | can you modify self directly? Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-09 16:18 -0600
  Re: Recursive class | can you modify self directly? Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-09 16:20 -0600
    Re: Recursive class | can you modify self directly? Russel Walker <russ.pobox@gmail.com> - 2013-07-10 02:00 -0700
  Re: Recursive class | can you modify self directly? Dave Angel <davea@davea.name> - 2013-07-09 18:30 -0400
  Re: Recursive class | can you modify self directly? Ethan Furman <ethan@stoneleaf.us> - 2013-07-09 15:19 -0700
  Re: Recursive class | can you modify self directly? Russel Walker <russ.pobox@gmail.com> - 2013-07-10 01:58 -0700
    Re: Recursive class | can you modify self directly? Terry Reedy <tjreedy@udel.edu> - 2013-07-10 15:33 -0400
      Re: Recursive class | can you modify self directly? Russel Walker <russ.pobox@gmail.com> - 2013-07-10 13:09 -0700
  Re: Recursive class | can you modify self directly? Russel Walker <russ.pobox@gmail.com> - 2013-07-10 15:50 -0700
    Re: Recursive class | can you modify self directly? Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-10 17:39 -0600

csiph-web