473,394 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

problem with Python class creating

Hi all,
I have the code like this one:

from myMisc import ooIter
class MyClass:
def __init__(self): pass
iterfcn = lambda *args: ooIter(self) # i.e pass the class instance
to other func named ooIter
field2 = val2
field3 = val3 # etc

So it yields "global name 'self' is not defined", that is true. How
could I handle the situation?

Currently I do (and it works, but give me some troubles - I should
call MyClass.__init__ for each children class, and there are lots of
those ones)

class MyClass:
def __init__(self):
iterfcn = lambda *args: ooIter(self) # i.e pass the class
instance to other func named ooIter
field2 = val2
field3 = val3 # etc

I suspect it has better solution, is it?
Thank you in advance, Dmitrey

Oct 18 '07 #1
3 1276
On Oct 19, 8:22 am, dmitrey <dmitrey.kros...@scipy.orgwrote:
Hi all,
I have the code like this one:

from myMisc import ooIter
class MyClass:
def __init__(self): pass
iterfcn = lambda *args: ooIter(self) # i.e pass the class instance
to other func named ooIter
field2 = val2
field3 = val3 # etc

So it yields "global name 'self' is not defined", that is true. How
could I handle the situation?

Currently I do (and it works, but give me some troubles - I should
call MyClass.__init__ for each children class, and there are lots of
those ones)

class MyClass:
def __init__(self):
iterfcn = lambda *args: ooIter(self) # i.e pass the class
instance to other func named ooIter
field2 = val2
field3 = val3 # etc

I suspect it has better solution, is it?
Thank you in advance, Dmitrey
without having tested - I think this should work for you:

from myMisc import ooIter

class MyClass:
def __init__(self): pass
iterfcn = lambda self: ooIter(self)

Oct 18 '07 #2
dmitrey a écrit :
Hi all,
I have the code like this one:

from myMisc import ooIter
class MyClass:
Unless you have a need for compatibility with aged Python versions,
you'd be better using new-style classes:

class MyClass(object):
def __init__(self): pass
This is the default behaviour, so you may as well get rid of this line.
iterfcn = lambda *args: ooIter(self) # i.e pass the class instance
to other func named ooIter
cf below about this...
field2 = val2
field3 = val3 # etc
You're aware that these two attributes are *class* attributes (that is,
shared by all instances) ?
So it yields "global name 'self' is not defined", that is true.
Indeed.
How
could I handle the situation?

iterfcn = lambda self: ooIter(self)

which could as well be written:

def iterfcn(self):
ooIter(self)
Remember that Python's methods are - at least at this stage - plain
functions, so the 'self' parameter is *not* optional - else how could
the function access the current instance ?

And FWIW, you don't need the *args if you don't use any other than 'self'.
Currently I do (and it works, but give me some troubles - I should
call MyClass.__init__ for each children class,
Not unless these classes define their own initializers. But that's
another problem
and there are lots of
those ones)

class MyClass:
def __init__(self):
iterfcn = lambda *args: ooIter(self)
The real problem is that you create one anonymous function *per instance*.
I suspect it has better solution, is it?
Indeed.
Oct 18 '07 #3
Thanks all for these detailed explanations.
On Oct 18, 10:48 pm, Bruno Desthuilliers
<bdesth.quelquech...@free.quelquepart.frwrote:
dmitrey a écrit :
Not unless these classes define their own initializers. But that's
another problem
and there are lots of
those ones)
class MyClass:
def __init__(self):
iterfcn = lambda *args: ooIter(self)

The real problem is that you create one anonymous function *per instance*.
No, it's not a problem - it's desired behaviour.
Regards, Dmitrey

Oct 19 '07 #4

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

Similar topics

0
by: RJS | last post by:
Hi all, I can't get a py2exe compiled app to run with numarray (numarray-0.5.win32- py2.2). Also wxPythonWIN32-2.3.3.1-Py22 and ActivePython-2.2.1-222. In the sample below, commenting out...
0
by: Bruce Davis | last post by:
I'm having a problem on windows (both 2000 and XP) with a multi-threaded tkinter gui application. The problem appears to be a deadlock condition when a child thread pops up a Pmw dialog window in...
8
by: Dan Perl | last post by:
Here is a problem I am having trouble with and I hope someone in this group will suggest a solution. First, some code that works. 3 classes that are derived from each other (A->B->C), each one...
10
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. ...
4
by: Vedanta Barooah | last post by:
greetings.... in a python nested class is it possible to change the value of the parent class's variable without actually creating an instance of the parent class, consider this code: class...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
6
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any...
122
by: Edward Diener No Spam | last post by:
The definition of a component model I use below is a class which allows properties, methods, and events in a structured way which can be recognized, usually through some form of introspection...
3
by: ShambhuHubli | last post by:
Hi all!! .. I am New member to this group. And also new to C/PYTHON API coding. I am trying to have two way communication i,e i am calling from python to C and then from C to python. I have no...
13
by: Rafe | last post by:
Hi, I am in a situation where I feel I am being forced to abandon a clean module structure in favor of a large single module. If anyone can save my sanity here I would be forever grateful. My...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.