top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration
Print Preview

Recursive class | can you modify self directly when using python

0 votes

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:

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?

posted Jul 9, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

1 Answer

0 votes

"self" is just a local binding of the object to the name self. You can rebind the name like this as with any other local variable, but that's all it does. It doesn't modify the object in any way, and no other bindings of the same object are affected.

If you actually want to modify the current object, you would need to do something like:

 def expand(self):
 import copy
 self.expr = Expr(self.expr, self.op, self.val)
 self.op = choice('+-*/')
 self.val = choice('12345')
answer Jul 9, 2013 by anonymous
Minus the "import copy".
Similar Questions
0 votes

I am extending a parser and need to create many classes that are all subclassed from the same object (defined in an external library). When my module is loaded I need all the classes to be created with a particular name but the behavior is all the same. Currently I have a bunch of lines like this:

 class Vspace(Base.Command): pass
 class Boldpath(Base.Command): pass

There are a bunch of lines like that. Is there a better way? Something like

 newclasses = ['Vspace', 'Boldpath', ... ]
 for name in newclasses:
 tmp = type(name, (Base.Command,) {})
 tmp.__name__ = name

Is there a more pythonic way?

0 votes

I can log into a web site with pexpect but what I want to do is pipe the opening window to a file. Logging into the site opens the site window but I can't get the window to a file.

I can't use screen capture I need to get pexpect to pipe it to a txt file.

0 votes

I have a list of a list of integers. The lists are long so i cant really show an actual example of on of the lists, but I know that they contain only the integers 1,2,3,4.

so for example. s2 = [[1,2,2,3,2,1,4,4],[2,4,3,2,3,1]]

I am calculating the product, sum, max, min.... of each list in s2 but I get negative or 0 for the product for a lot of the lists. (I am doing this in ipython)

for x in s2: Â  Â  print(len = , len(x), sum = , sum(x), prod = , prod(x), max = , max(x), min = , min(x))
 (len = , 100, sum = , 247, prod = , 0, max = , 4, min = , 1) (len = , 100, sum = , 230, prod = , -4611686018427387904, max = , 4, min = , 1) (len = , 100, sum = , 261, prod = , 0, max = , 4, min = , 1)
 (prod =, 0, max =, 4, min =, 1) (prod =, 1729382256910270464, max =, 4, min =, 1) (prod =, 0, max =, 4, min =, 1)

Whats going on?

Useful Links with Similar Problem
Contact Us
+91 9880187415
#470/147, 3rd Floor, 5th Main,
HSR Layout Sector 7,
Bangalore - 560102,
Karnataka INDIA.