Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 12 Online
OpenStudy (anonymous):

Can someone explain the finer points of classes? Particularly I'm interested in exactly how where instances of a class are stored. For instance... If I initiate an instance of a class without specifically giving it a variable name...how can I go back and access it? Is there a way to itterate through instances or do you have to tie to a variable name? Also, does Python use heaps/stacks like C++? Do I not see them because I'm always running my programs in once space? Thanks!

OpenStudy (anonymous):

if you do not give reference of class to specific class you can't really access it, but it still can be usefult, for example (in JAVA, maybe it differs in C++): System.out.println(new Date(2011, 12, 25)); would print out something what is defined in toString method in Date class I am pretty sure python has stacks heaps too, most programming languages should have. When you call function it hold parameters, local variables. And heap is for dynamic memory allocation so you have objects in python aswell

OpenStudy (anonymous):

The beginning of section nine of the tutorial has a bit on scope and namespaces http://docs.python.org/tutorial/classes.html#classes There is a bit at the end of: http://docs.python.org/reference/datamodel.html#the-standard-type-hierarchy The dir() function gives you a look at the local scope You can use a class without creating a separate instance - there is a use for that http://pastebin.com/DnNHN19h I don't use classes very often and I wondered if i just didn't get it until i read somewhere - if you need an object to maintain an internal state, use a class - otherwise use a function. That cleared it up a bit for me. maybe this was it - http://www.python.org/doc/essays/ppt/hp-training/tsld008.htm yep that whole presentation is pretty good

OpenStudy (anonymous):

@MPizzle: If you create an object and do not keep any references to it, in a managed language like Java or Python it will eventually be garbage collected (memory will be released by the environment back to the OS). If you do the same thing in C++, you have a memory leak, and it's considered to be a very serious bug. Where class instances are stored is up to the environment. It could be in automatic storage, or on the heap. It's implementation dependent. Understanding scope will help you to understand garbage collection, for example: If I declare an instance of a class in a method, and keep no external references to it, it will probably be garbage collected some time after the method exits in a managed language (python, java, C# etc) @Tomas.A If you do System.out.println(new Date(..)) you are assigning a reference in the scope of System.out.println to the new Date, via its parameter. So no, creating an object is typically *not* useful if you have no references to it. If it is, then maybe that particular code should *not* be in a constructor, but a method.

OpenStudy (anonymous):

but I created object and printed something to standard output and after that I can't access same object anymore or am I wrong?

OpenStudy (anonymous):

in Python if you 'create' a class in a module, that class will exist in the module's scope apart from any instance that is created. http://codepad.org/hmKDfJQi I have seen examples of the utility of using the class itself (rather than an instance) but I can't seem to find it right now.

OpenStudy (anonymous):

@Tomas.A If you have created an object, then passed the object to System.out.print, you have *given System.out.print a reference* to your object. This means your statement that no references were created is incorrect, however you are correct that you will not be able to access it later. However realize the more subtle ways of creating references. For example, adding a new instance directly to a HashMap will create a reference.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!