473,573 Members | 2,696 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1132
On Aug 17, 11:07 am, yadin <conra2...@yaho o.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.lis t and you can
get to the list:
print bigInstance.lis t;

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*******@yaho o.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.appe nd(y) # send the value to first list
#same with big.x
big.mylist.appe nd(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.myinstance list = []

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

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

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

print "Big.myclasslis t:", Big.myclasslist

console output:

D:\user\workspa ce\vg\tmptestin g>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***********@A cm.Org

Aug 18 '07 #5

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

Similar topics

8
7431
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, http://www.vbwm.com/articles/2002/abarfield/hta01/ I have a some questions about Figure 5 (below). This hta file will view a text file when a button is pushed. 1. Why do the...
2
4164
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 Router by programing ? Thanks
2
10536
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 world. My question was: if I have a simple program that for example calculates the squares of 2 to 100 times, how can I write the resulting output...
3
1583
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 donate serchengine
12
3573
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 utilities that allows you to view them with live rotation in a web browser. Also, it includes a introductory tutorial to POV-Ray.
8
1910
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 numbers we entered to a..that should be added.. for eg if a = 34 the answe should be 7 that is 3+4 using % and / we can solve this problem..pls help...
4
1623
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 show ERROR – Incorrect color – try again: I'm tring to figure out if this is a while loop or a do while loop. and help would be great. Here...
2
1601
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 give me.
0
7750
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7989
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8182
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7744
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8039
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5559
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3708
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2170
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1271
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.