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

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???

Aug 17 '07 #1
4 1125
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: chuck clark | last post by:
Hi, I am sorry if this is not the most appropriate group to ask this in (if not, please point me in the right direction). I am following examples from this page,...
2
by: cyshao | last post by:
How to reset Router by programing? For some resean, we need usually reset our Router. Now, we have to Reset Router manually(shot down and reopen). Are there any method to control and reset...
2
by: continium | last post by:
I have been learning python during the past weeks and I have been able to program some interesting things. Python is my first and only programing language from which I wish to enter the programing...
3
by: warhero721 | last post by:
HI. First i like to point out i suck at programing. All i realy whant to know is how do i git my site orginized.I whant to know how to do somthing like this Chatroom fourm sighn up event...
12
by: Xah Lee | last post by:
Of Interest: Introduction to 3D Graphics Programing http://xahlee.org/3d/index.html Currently, this introduction introduces you to the graphics format of Mathematica, and two Java Applet...
8
by: SanjaiGandhi | last post by:
Hi ...i am new to programing....pls help to overcome this program.. The Program is..: if a = 557..using for loop or while or dowhile ..we have to get the answer for 5+5+7..that is what ever...
4
nomad
by: nomad | last post by:
I'm very new to java and programing. I need some help with this. Check each user entry to ensure validity. Color is either Black, White, or Red. if the user enter the wrong color the following with...
2
by: sengpg345 | last post by:
hi everybody. i have a project in colage for messanger but i haven't idea of socket programing in c#.net . so plz give me the guidence i socket programing. if u have row matirial or tutorial then...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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.