473,385 Members | 1,492 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,385 software developers and data experts.

Question about classes and possible closure.

This is all an intro learning experience for me, so please feel free to
explain why what I'm trying to do is not a good idea.

In the Cookbook, they have a recipe for how to create global constants.

-----------------
class _const:
class ConstError(TypeError): pass
def __setattr__(self,name,value):
if self.__dict__.has_key(name):
raise self.ConstError, "Can't rebind const(%s)"%name
self.__dict__[name]=value

import sys
sys.modules[__name__]=_const()
----------------

I'd like to be able to create constants within a class. (Yes I understand
that uppercase names is a far better and easier convention but this is a
learning experience for me.)

I can't get this to work, but my idea is that MyClass is defined thusly:

class ConstError(TypeError): pass
class Myclass:
def mprint(self):
print "C1 = ", self._C1

# Define a subclass to create constants. It refs upself to access
# the uplevel copy of self.
class _const:
class ConstError(TypeError): pass
def __setattr__(_upself,name,value):
if upself.__dict__.has_key(name):
raise self.ConstError, "Can't rebind const(%s)"%name
else:
print "Just set something"
upself.__dict__[name]=value

# I want the const instance to be contained in this class so I
# instantiate here in the constructor.
def __init__(self):
upself = self
upself.consts = const()
upself.consts._C1 = 0
setattr(upself.consts, "_C1", 44)
self = upself

Then the call in another file is this:
#! /usr/bin/python
from c2 import Myclass
foo = Myclass()
foo.mprint()
# end

Is it possible to nest a class in another class and is it possible to make
this work?

TIA

--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net
Feb 21 '07 #1
3 1240
On Feb 21, 4:30 pm, "Steven W. Orr" <ste...@syslang.netwrote:
This is all an intro learning experience for me, so please feel free to
explain why what I'm trying to do is not a good idea.

In the Cookbook, they have a recipe for how to create global constants.

-----------------
class _const:
class ConstError(TypeError): pass
def __setattr__(self,name,value):
if self.__dict__.has_key(name):
raise self.ConstError, "Can't rebind const(%s)"%name
self.__dict__[name]=value

import sys
sys.modules[__name__]=_const()
----------------

I'd like to be able to create constants within a class. (Yes I understand
that uppercase names is a far better and easier convention but this is a
learning experience for me.)

I can't get this to work, but my idea is that MyClass is defined thusly:

class ConstError(TypeError): pass
class Myclass:
def mprint(self):
print "C1 = ", self._C1
Looking ahead, this should be "self.consts._C1", not "self._Cl".
# Define a subclass to create constants. It refs upself to access
# the uplevel copy of self.
It's better to stick with the conventions. But regardless, you have to
be consistent, as we see in a moment.
class _const:
class ConstError(TypeError): pass
This is redundant. Even though this ConstError and the one defined
before the Myclass definition are in completely different scopes, so
there's no conflict, I doubt this is what you meant to do.
def __setattr__(_upself,name,value):
Notice your first argument here is "_upself", with an underscore.
if upself.__dict__.has_key(name):
Now you're using "upself", without an underscore, which is undefined.
raise self.ConstError, "Can't rebind const(%s)"%name
Now you're using "self", which is also undefined.
else:
print "Just set something"
upself.__dict__[name]=value
Once again, "upself" instead of "_upself".

Just follow the convention: always use "self".
>
# I want the const instance to be contained in this class so I
# instantiate here in the constructor.
def __init__(self):
upself = self
Why "upself = self"? You could just as easily use "self". Follow the
conventions.
upself.consts = const()
I'm guessing this is your error? That should be "upself.consts =
self._const()"
upself.consts._C1 = 0
setattr(upself.consts, "_C1", 44)
Obviously, this is going to raise an error, because you're redefining
_Cl.
self = upself
Again, why the weird juggling between self and upself? First of all,
self and upself are references to the same object right now, so "self
= upself" has no after-effect at all. Second of all, even if it did,
all it would be doing is reassigning the local variable "self" to a
different object; it has no effect on the object you're working with.
Then the call in another file is this:
#! /usr/bin/python
from c2 import Myclass
foo = Myclass()
foo.mprint()
# end

Is it possible to nest a class in another class and is it possible to make
this work?
Yes, it is possible to nest a class in another class. Yes, it is
possible to make this work. However, it is good practice to post your
error message along with your code, so we don't have to guess.

Here's an untested rewrite of your code:

class Myclass:
def mprint(self):
print "C1 = ", self.consts._C1

# Define a subclass to create constants. It refs upself to access
# the uplevel copy of self.
class _const:
class ConstError(TypeError): pass
def __setattr__(_upself,name,value):
if _upself.__dict__.has_key(name):
raise _upself.ConstError, "Can't rebind
const(%s)"%name
else:
print "Just set something"
_upself.__dict__[name]=value

# I want the const instance to be contained in this class so I
# instantiate here in the constructor.
def __init__(self):
self.consts = const()
self.consts._C1 = 0
# This will raise an error!!!!
setattr(self.consts, "_C1", 44)
TIA

--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net
Feb 21 '07 #2
Steven W. Orr wrote:
This is all an intro learning experience for me, so please feel free to
explain why what I'm trying to do is not a good idea.

In the Cookbook, they have a recipe for how to create global constants.

-----------------
class _const:
class ConstError(TypeError): pass
def __setattr__(self,name,value):
if self.__dict__.has_key(name):
raise self.ConstError, "Can't rebind const(%s)"%name
self.__dict__[name]=value

import sys
sys.modules[__name__]=_const()
----------------

I'd like to be able to create constants within a class. (Yes I
understand that uppercase names is a far better and easier convention
but this is a learning experience for me.)

I can't get this to work, but my idea is that MyClass is defined thusly:

class ConstError(TypeError): pass
class Myclass:
def mprint(self):
print "C1 = ", self._C1

# Define a subclass to create constants. It refs upself to access
# the uplevel copy of self.
class _const:
class ConstError(TypeError): pass
def __setattr__(_upself,name,value):
if upself.__dict__.has_key(name):
raise self.ConstError, "Can't rebind const(%s)"%name
else:
print "Just set something"
upself.__dict__[name]=value

# I want the const instance to be contained in this class so I
# instantiate here in the constructor.
def __init__(self):
upself = self
upself.consts = const()
upself.consts._C1 = 0
setattr(upself.consts, "_C1", 44)
self = upself

Then the call in another file is this:
#! /usr/bin/python
from c2 import Myclass
foo = Myclass()
foo.mprint()
# end

Is it possible to nest a class in another class and is it possible to
make this work?

TIA

I see no reason to nest classes, ever, as each creates a seperate name
space. Better would be to de-nest _const and make it available to all of
your classes, otherwise you have lost some of its reusability. A class
is esentially the definition of a behavior and so nesting assumes that
the nesting confers upon the outer class that having a nested class
changes its behavior. This is what composition is for. If you want a
reference to a class in your code, then make an assignment:
# Const.py
class ConstError(TypeError): pass

class Const:
def __setattr__(self, name, value):
if hasattr(self, name):
raise ConstError, "Can't rebind const(%s)" % name
else:
print "Just set something"
self.__dict__[name] = value
# Myclass.py
import Const

class Myclass:

def mprint(self):
print "C1 = ", self.consts._C1

def __init__(self):
self.consts = Const()
self.consts._C1 = 0
# This will raise an error!!!!
self.consts._C1 = 44
This makes a lot more sense and follows the "flat is better than nested"
rule. Notice how, otherwise, ConstError would be triple nested. Now
you can import Const and have constants in all your classes.

I also pythonized some of your code.

James
Feb 22 '07 #3
James Stroud wrote:
Steven W. Orr wrote:
>This is all an intro learning experience for me, so please feel free
to explain why what I'm trying to do is not a good idea.

In the Cookbook, they have a recipe for how to create global constants.

-----------------
class _const:
class ConstError(TypeError): pass
def __setattr__(self,name,value):
if self.__dict__.has_key(name):
raise self.ConstError, "Can't rebind const(%s)"%name
self.__dict__[name]=value

import sys
sys.modules[__name__]=_const()
----------------

I'd like to be able to create constants within a class. (Yes I
understand that uppercase names is a far better and easier convention
but this is a learning experience for me.)

I can't get this to work, but my idea is that MyClass is defined thusly:

class ConstError(TypeError): pass
class Myclass:
def mprint(self):
print "C1 = ", self._C1

# Define a subclass to create constants. It refs upself to access
# the uplevel copy of self.
class _const:
class ConstError(TypeError): pass
def __setattr__(_upself,name,value):
if upself.__dict__.has_key(name):
raise self.ConstError, "Can't rebind const(%s)"%name
else:
print "Just set something"
upself.__dict__[name]=value

# I want the const instance to be contained in this class so I
# instantiate here in the constructor.
def __init__(self):
upself = self
upself.consts = const()
upself.consts._C1 = 0
setattr(upself.consts, "_C1", 44)
self = upself

Then the call in another file is this:
#! /usr/bin/python
from c2 import Myclass
foo = Myclass()
foo.mprint()
# end

Is it possible to nest a class in another class and is it possible to
make this work?

TIA


I see no reason to nest classes, ever, as each creates a seperate name
space. Better would be to de-nest _const and make it available to all of
your classes, otherwise you have lost some of its reusability. A class
is esentially the definition of a behavior and so nesting assumes that
the nesting confers upon the outer class that having a nested class
changes its behavior. This is what composition is for. If you want a
reference to a class in your code, then make an assignment:
# Const.py
class ConstError(TypeError): pass

class Const:
def __setattr__(self, name, value):
if hasattr(self, name):
raise ConstError, "Can't rebind const(%s)" % name
else:
print "Just set something"
self.__dict__[name] = value
# Myclass.py
import Const

class Myclass:

def mprint(self):
print "C1 = ", self.consts._C1

def __init__(self):
self.consts = Const()
self.consts._C1 = 0
# This will raise an error!!!!
self.consts._C1 = 44
This makes a lot more sense and follows the "flat is better than nested"
rule. Notice how, otherwise, ConstError would be triple nested. Now you
can import Const and have constants in all your classes.

I also pythonized some of your code.

James
should be self.consts = Const.Const()

James
Feb 22 '07 #4

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

Similar topics

27
by: Ted Lilley | last post by:
What I want to do is pre-load functions with arguments by iterating through a list like so: >>>class myclass: .... pass >>>def func(self, arg): .... print arg >>>mylist = >>>for item...
20
by: svend | last post by:
I'm messing with some code here... Lets say I have this array: a1 = ; And I apply slice(0) on it, to create a copy: a2 = a1.slice(0); But this isn't a true copy. If I go a1 = 42, and then...
7
by: cwhite | last post by:
The answer to this is probably very simple, but it has been a while since I have had to use access. I have two different queries, one counts records: SELECT Closure.Closed_As,...
3
by: Sam Kong | last post by:
Hi group, I want to have some advice about immutable objects. I made a constructor. function Point(x, y) { this.x = x; this.y = y; }
7
by: Csaba Gabor | last post by:
I feel like it's the twilight zone here as several seemingly trivial questions are bugging me. The first of the following three lines is a syntax error, while the last one is the only one that...
4
by: emailscotta | last post by:
Below are some over simplified examples of code the create a single instance of an object and I was hoping some one could give me the pros and cons of each approach. First declaration: In this...
26
by: Cliff Williams | last post by:
Can someone explain the pros/cons of these different ways of creating a class? // 1 function myclass() { this.foo1 = function() {...} } // 2a
11
by: Huayang Xia | last post by:
What will the following piece of code print? (10 or 15) def testClosure(maxIndex) : def closureTest(): return maxIndex maxIndex += 5 return closureTest()
18
by: Tom Cole | last post by:
I'm working on a small Ajax request library to simplify some tasks that I will be taking on shortly. For the most part everything works fine, however I seem to have some issues when running two...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.