473,486 Members | 1,560 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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

Feb 19 '07 #1
5 1083
op*****@ukr.net wrote:
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:
>>class Primal:
.... pass
....
>>class A:
.... def __init__(self, fun, x0):
.... self.primal = Primal()
.... self.primal.f = fun
....
>>def square(x): return x*x
....
>>a = A(square, 42)
a.primal.f
<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

Feb 19 '07 #2
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
Feb 19 '07 #3
op*****@ukr.net wrote:
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
>>class Struct:
.... def __init__(self, **kw):
.... self.__dict__.update(kw)
....
>>a = Struct(f=42)
b = Struct(x=1, y=2)
a.f
42
>>b.x, b.y
(1, 2)

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

Peter
Feb 19 '07 #4
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
Feb 19 '07 #5
op*****@ukr.net wrote:
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::
>>A = type('', (), {})
a = A()
a
<__main__. object at 0x009E8490>
>>a.foo = 42
a.foo
42

But perhaps using this (with a better name) would be more sensible
(according to readability)::
>>class B: pass
....
>>b = B()
b.foo = 42
b.foo
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::
>>class Autocreating(object):
.... def __getattr__(self, attr):
.... if hasattr(self, attr)
.... setattr(self, attr, Autocreating())
.... return getattr(self, attr)
....
>>a = Autocreating()
a.foo = 42
a.foo
42
>>a.hello.world = 23
a.hello
<__main__.Autocreating object at 0x...>
>>a.hello.world
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
Feb 19 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3432
by: Brian Angliss | last post by:
I'm relatively new to scripting in JavaScript, so I'm not too surprised I'm having difficulty scripting up an animation effect for my personal site. What I'm trying to do is the following: When...
5
3163
by: overbored | last post by:
I can do this: int asdf; int* zxcv = asdf; but not this: int asdf; int** zxcv = asdf;
1
3264
by: Joseph Czapski | last post by:
Hi. You know that special character \b (word border) that is handy to use because it does not eat a character that you might want to match to another piece of your expression? \b matches the...
6
1152
by: Shawn B. | last post by:
Greetings, I have a troubling issue that I'm not sure how to approach at this point. Given the HTML tag (any tag will do): <div id='divSomething' onmouseover='...'>Next we write...
3
1356
by: bklopfen | last post by:
I am trying to create XSLT to convert XML like this: <?xml version="1.0" encoding="UTF-8"?> <sampledoc> <asdf mytoken="name1234"> <qwer>value1234</qwer> </asdf> <asdf...
7
5073
by: dmitrey | last post by:
hi all, can anyone explain howto get function from module, known by string names? I.e. something like def myfunc(module_string1, func_string2, *args): eval('from ' + module_string1 + 'import...
8
3740
by: dmitrey | last post by:
howto check does module 'asdf' exist (is available for import) or no? (without try/cache of course) Thx in advance, D.
3
303
by: Nathan Moinvaziri | last post by:
I am wonder if there is a way to use preprocessor definitions to expose code only if a particular file is included in a project. I am targeting the msvc. I am thinking of something like #if...
12
2994
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6839
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7305
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5427
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4863
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
259
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.