(beginners question) howto set self.field4.subfield8='asdf'?

O

openopt

I have
class A:
def __init__(self, objFun, x0):
#(I want to have self.primal.f = objFun)
#both
self.primal.f = objFun
#and
self.primal = None
self.primal.f = objFun

yields error
what should I do?
Thx
 
P

Peter Otten

I have
class A:
def __init__(self, objFun, x0):
#(I want to have self.primal.f = objFun)
#both
self.primal.f = objFun
#and
self.primal = None
self.primal.f = objFun

None is a singleton, so if Python were to allow the above f would always be
the objFun of the last created A instance.
yields error
what should I do?

Make a dedicated Primal class:
.... pass
........ def __init__(self, fun, x0):
.... self.primal = Primal()
.... self.primal.f = fun
....<function square at 0x401d609c>

Even better, because it makes no assumptions about the internal layout of
Primal:

class Primal:
def __init__(self, f):
self.f = f

....
self.primal = Primal(fun)
....

Peter
 
O

openopt

Thx
but is there any simpleir way, if using not class, but just struct (or
something like that, MATLAB equivalent for that one)?
I'm thinking of rewriting some optimization solvers (non-smooth,
constrained, with (sub)gradients or patterns provided by user) to
Python and I don't know currently is it possible to easy convert
things like
prob = [];
prob.advanced.ralg.hs = 1 (so in this line all subfields are
generating automatically in MATLAB or Octave)
I have huge amount of such lines, and implementing separate class for
each one is unreal.
Thank you in advance, Dmitrey
 
P

Peter Otten

Thx
but is there any simpleir way, if using not class, but just struct (or
something like that, MATLAB equivalent for that one)?
I'm thinking of rewriting some optimization solvers (non-smooth,
constrained, with (sub)gradients or patterns provided by user) to
Python and I don't know currently is it possible to easy convert
things like
prob = [];
prob.advanced.ralg.hs = 1 (so in this line all subfields are
generating automatically in MATLAB or Octave)
I have huge amount of such lines, and implementing separate class for
each one is unreal.

Get used to thinking of a class as a lightweight construct written with
well-defined constraints rather than some know-it-all can-do-everything
unwieldy monster. You can of course use one class throughout
.... def __init__(self, **kw):
.... self.__dict__.update(kw)
....(1, 2)

but separate classes don't require much more effort and make your code both
more readable and more flexible.

Peter
 
O

openopt

Thx
but is there any simpleir way, if using not class, but just struct (or
something like that, MATLAB equivalent for that one)?
I'm thinking of rewriting some optimization solvers (non-smooth,
constrained, with (sub)gradients or patterns provided by user) to
Python and I don't know currently is it possible to easy convert
things like
prob = [];
prob.advanced.ralg.hs = 1 (so in this line all subfields are
generating automatically in MATLAB or Octave)
I have huge amount of such lines, and implementing separate class for
each one is unreal.
Thank you in advance, Dmitrey
 
S

Stargaming

Thx
but is there any simpleir way, if using not class, but just struct (or
something like that, MATLAB equivalent for that one)?

Use this::
42

But perhaps using this (with a better name) would be more sensible
(according to readability)::
42

I'm thinking of rewriting some optimization solvers (non-smooth,
constrained, with (sub)gradients or patterns provided by user) to
Python and I don't know currently is it possible to easy convert
things like
prob = [];
prob.advanced.ralg.hs = 1 (so in this line all subfields are
generating automatically in MATLAB or Octave)

Perhaps something like this would be suitable::
.... def __getattr__(self, attr):
.... if hasattr(self, attr)
.... setattr(self, attr, Autocreating())
.... return getattr(self, attr)
....23

But this is perhaps not a good way because it's way too implicite.
I have huge amount of such lines, and implementing separate class for
each one is unreal.

You don't have to implement a separate class for *each one*. Use one
class for every attribute, you can reuse it. But perhaps something like
a dict is better for your purposes, anyways.

HTH,
Stargaming
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top