Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   help on object programing (http://www.velocityreviews.com/forums/t529997-help-on-object-programing.html)

yadin 08-17-2007 05:07 PM

help on object programing
 
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???


mike 08-17-2007 05:37 PM

Re: help on object programing
 
On Aug 17, 11:07 am, yadin <conra2...@yahoo.com> wrote:
> 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/



Neil Cerutti 08-17-2007 06:38 PM

Re: help on object programing
 
On 2007-08-17, yadin <conra2004@yahoo.com> wrote:
> 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

DouhetSukd@gmail.com 08-17-2007 08:32 PM

Re: help on object programing
 

> 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



Scott David Daniels 08-18-2007 04:55 AM

Re: help on object programing
 
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
Scott.Daniels@Acm.Org





All times are GMT. The time now is 07:17 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.