top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

Python: Is there way to get list of instances of particular class through class itself?

+3 votes
101 views

Is there way to get list of instances of particular class through class itself? via metaclass or any other method?

If class is object is it possible to delete it? If it is possible then how instances of that class will behave?

posted Jan 24, 2014 by Bob Wise

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+2 votes

Ans 1: Not automatically, but you can make a class that keeps track of its instances with a weak reference system.

Ans 2: It's possible to unbind the name, but every instance retains a reference to its class, so the class itself won't disappear until there are no instances left of it.

answer Jan 24, 2014 by Kiran Kumar
Similar Questions
+2 votes

I would like to apply the Pool.map method to a member of a class. Here is a small example that shows what I would like to do:

from multiprocessing import Pool

class A(object):
 def __init__(self,x):
 self.value = x
 def fun(self,x):
 return self.value**x

l = range(10)

p = Pool(4)

op = p.map(A.fun,l)

using this with the normal map doesn't cause any problem

This fails because it says that the methods can't be pickled. (I assume it has something to do with the note in the documentation: "functionality within this package requires that the __main__ module be importable by the children.", which is obscure to me).

I would like to understand two things: why my code fails and when I can expect it to fail? what is a possible workaround?

0 votes

I have a hierarchy of classes (SubA, SubAB, SubB, ..., SubBCA, SubC,...), each of which is inheriting from a chain of superclasses with a common baseclass(Sup) on top. (So far, no problem)

Now, I want to create instances of the correct subclasstype as decided by the common baseclass, like this:

i = Sup(args_allowing_the_baseclass_to_deduce_correct_subclass)

where i can be of any class except Sup itself (as decided by Sup)

Now, the problem:

How to design the __new__() and __init__() methods for the various classes in order to achieve what I want?

+1 vote

I want to build a class that perform various functions to the content(or lines) of any given file. I want to first include the opening and reading file function into that class, then add other methods.

Here is what I wrote for opening file and reading the lines:

class FileOpration:
 def __init__(self,name):
 self.name = name
 self.filename = self.name
 def readAllline(self):
 open(self.name).readlines()

file1 = FileOpration("myfile.txt")
print file1.filename
allines = file1.readAllline()
print allines

I define self.name variable because I want to store the specific file that I read. This works fine by the test code of print file1.filename. I saw "myfile.txt" on the terminal output.

However, the readAllline() method dose not work. The code only give me "None" as the output on terminal

+3 votes

Is there a way to append a list of files as an executable to the file itself such that the file is still executable?

For example let's say I have 2 files file1 and file2. How might I append the the files in a way to the binary. Again this is a simple example, I would like to have a cross Operating System way to extract Unix and Windows functions. Cross operating system way to append zips (or tar.gz) to a file only to extract it later.

The following illustrates how the files might be extracted and or created.

create ./a.exe ./output.exe file1 file2

extract output.exe
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...