By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,286 Members | 1,169 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,286 IT Pros & Developers. It's quick & easy.

help on object programing

P: n/a
class big(self):

x = 32
list = []
def inside (self):

class small(self): # a new class defined inside the first

y = 348
list.append(y) # send the value to first list
list.append(x)

print list

how can i define my variables so that there are valid outside the
class???

Aug 17 '07 #1
Share this Question
Share on Google+
4 Replies

P: n/a
On Aug 17, 11:07 am, yadin <conra2...@yahoo.comwrote:
class big(self):

x = 32
list = []
def inside (self):

class small(self): # a new class defined inside the first

y = 348
list.append(y) # send the value to first list
list.append(x)

print list

how can i define my variables so that there are valid outside the
class???

Well, first you have to create an instance of the class big:
bigInstance = big();

then to get to bigInstance's list you do bigInstance.list and you can
get to the list:
print bigInstance.list;

but the list will be empty.

check out the dive into python book to understand it object oriented
programming a little more.
http://www.diveintopython.org/
Aug 17 '07 #2

P: n/a
On 2007-08-17, yadin <co*******@yahoo.comwrote:
class big(self):

x = 32
list = []
def inside (self):

class small(self): # a new class defined inside the first

y = 348
list.append(y) # send the value to first list
list.append(x)

print list

how can i define my variables so that there are valid outside
the class???
Be sure to read and try the code in the sections of the Python
tutorial that discuss classes and the objects they create.

In my opinion the classes section of the official tutorial is
unfortunately the least tutorial part of the tutorial. But it
won't steer you wrong.

--
Neil Cerutti
Aug 17 '07 #3

P: n/a
how can i define my variables so that there are valid outside the
class???
Not to be obnoxious, but your sample code has a number of fairly big
conceptual issues (like subclassing self and assigning 32 at the big
class level and printing 'list' which is a built-in type). Been there
myself - it took me a while to understand class vs. instance
variables. You are also using fairly advanced techniques such as
embedded classes which look above a newbie level.

I took the liberty to comment and "fix" up your code a bit so that it
runs:

#classes either inherit or not and signify that in parenthesis.
inheriting 'self' makes no sense
#for a class declaration. but it does make perfect sense to have
'self' as a param to a class method (def)

class big:

#class-level variable
x = 32

#changed to mylist to avoid confusion with list built-in.
mylist = []

#this is a normal instance method - 'self' refers to the class
instance you just created.
def inside (self):

#is this really what you wanted? an embedded class -
syntaxically correct, but not used often
class small: # a new class defined inside the first

y = 348
#ok, now I am referring to the mylist variable associated
(bound in python-speak) to the big class.
#y is defined here so no need to do anything
big.mylist.append(y) # send the value to first list
#same with big.x
big.mylist.append(big.x)
#instantiate the class, because you are calling an instance method
(i.e. you need to have created an instance to use that method)
#call the call
mybig = big().inside()

#refer to the mylist variable declared at the class, not instance
level.
#class level means any other calls you make will alter that variable
print 'my class level mylist variable:',big.mylist

console output:

my class level mylist variable: [348, 32]

Can you perhaps rephrase your requirements to indicate what you want
to achieve?

Strictly speaking, it looks like you could do this:

class Big:
def getlist(self):
return [348,32]

mylist =Big().getlist()

That's probably not what you were asking for, but it does express the
results you would get out of your code, especially as you are not
passing in any significant parameters to the 'inside' function.

OK, perhaps a bit more useful.

#no inheritance - could also be class Big(object) where object is the
python "root class"
class Big:

#initialize the class level to be empty
myclasslist = []
def __init__(self):
#initialize the instance level variable to be empty
self.myinstancelist = []

def appendAndGet(self,x,y):
#modify the instance's "personal" list
self.myinstancelist.append(x)
self.myinstancelist.append(y)
#will now modify shared class-level variable.
Big.myclasslist.append(x)
Big.myclasslist.append(y)
return self.myinstancelist
print "Big.myclasslist without any instances around:", Big.myclasslist
bigA = Big()
result = bigA.appendAndGet(348,32)
print "result #1:", result
print "Big.myclasslist:", Big.myclasslist

bigB = Big()
result = bigB.appendAndGet(11,22)
print "result #2:", result

#and the instance also still has its myinstancelist around
print "same as result #2:", bigB.myinstancelist

print "Big.myclasslist:", Big.myclasslist

console output:

D:\user\workspace\vg\tmptesting>testc.py
my class level mylist variable: [348, 32]
Big.myclasslist without any instances around: []
result #1: [348, 32]
Big.myclasslist: [348, 32]
result #2: [11, 22]
same as result #2: [11, 22]
Big.myclasslist: [348, 32, 11, 22]

Try perhaps Dive Into Python's class intro:

http://www.diveintopython.org/object...g_classes.html

Cheers
Aug 17 '07 #4

P: n/a
yadin wrote:
class big(self):
...
how can i define my variables so that there are valid outside the
class???
Although you did not describe the issue all that well, I _think_ you
are referring to the fact that the following two pieces of code behave
less like each other than one might suspect. The previous responders
have missed the confusing part of python in your attempt to present your
confusion.

def big():
abc = 7
print abc
class small(object):
print '...'
print abc
def __init__(self):
print abc
return small()
big()

class Big(object):
Abc = 13
print Abc
class Small(object):
print '...'
print Abc
def __init__(self):
print Abc
cell = Small()
big()

The explanation is unfortunately a little technical. The
class-in-a-function (small) is not defined until the function
(big) is actually _called_, not when big is being defined. The
class-in-a-class (Small) in the second instance is actually
defined _before_ Big is being defined, and the temporary
namespace that contains Abc is hidden while Small is being
defined.

-Scott David Daniels
Sc***********@Acm.Org

Aug 18 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.