Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #50281
| Date | 2013-07-09 15:19 -0700 |
|---|---|
| From | Ethan Furman <ethan@stoneleaf.us> |
| Subject | Re: Recursive class | can you modify self directly? |
| References | <61751485-a298-403e-8b44-be7cf2504f0e@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4478.1373410388.3114.python-list@python.org> (permalink) |
On 07/09/2013 03:01 PM, Russel Walker wrote:
>
> 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'))
`self` is just a name. In `expand()` you are rebinding the name `self` away from the object and to a new Expr instance.
If you want to change `self` the original object you have to do something like:
def expand(self):
self.op = choice('+-*/')
self.val = choice('12345')
--
~Ethan~
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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