I am having big trouble with classes. I don’t think I really understand them and sourly not as clear as functions. I do know that they are an important part of programming so I appreciate all the help I can get to get things clearer.

I am trying to rewrite this to one big calls but I doesn’t seem to work.

``````import random

def fac(n):
if n == 0:
return 1
else:
return (fac(n-1) * n)

def fel(antal):
n = len(antal)
if n==0: yield []
else:
for i in range(len(antal)):
for cc in fel(antal[:i]+antal[i+1:]):
yield [antal[i]]+cc

def k_fel(antal, n):
if n==0: yield []
else:
for i in range(len(antal)):
for ss in k_fel(antal, n-1):
if (not antal[i] in ss):
yield [antal[i]]+ss

def r_fel(list):
längd = len(list);
max = fac(längd);
index = random.randrange(0, max)
i = 0
for p in fel(list):
if i == index:
return p
i += 1

def alla_värden(värde, platser):
värde = r_fel(värde)

for s in k_fel(värde, platser):

yield(s)

print (lista)
``````

And this is my go at calsses.

``````import random

class alla_värden (self, värde, antal):

self.värde = r_fel(self.värde)

for s in k_fel(värde, platser):
yield(s)

def __int__(self, värde, antal):
self.värde = värde
self.antal = antal

def fac(self, n):
if n == 0:
return 1
else:
return (fac(n-1) * n)

def fel (self, antal):
n = len(antal)
if n==0: yield []
else:
for i in range(len(antal)):
for cc in fel(antal[:i]+antal[i+1:]):
yield [antal[i]]+cc

def k_fel (self, antal, n):
if n==0: yield []
else:
for i in range(len(antal)):
for ss in k_fel(antal, n-1):
if (not antal[i] in ss):
yield [antal[i]]+ss

def r_fel(self, list):

längd = len(list);
max = fac(längd);
index = random.randrange(0, max)
i = 0
for p in fel(list):
if i == index:
return p
i += 1
``````

It does not work due to error: yield outside function.

## All 6 Replies

Ok it seams that i cant have argument in the class.

``````import random

class alla_värden:

def __init__(self, värde, antal):
self.värde = värde
self.antal = antal

def fac(self, n):
if n == 0:
return 1
else:
return (fac(n-1) * n)

def fel (self, antal):
n = len(antal)
if n==0: yield []
else:
for i in range(len(antal)):
for cc in fel(antal[:i]+antal[i+1:]):
yield [antal[i]]+cc

def k_fel (self, antal, n):
if n==0: yield []
else:
for i in range(len(antal)):
for ss in k_fel(antal, n-1):
if (not antal[i] in ss):
yield [antal[i]]+ss

def r_fel(self, list):

längd = len(list);
max = fac(längd);
index = random.randrange(0, max)
i = 0
for p in fel(list):
if i == index:
return p
i += 1

def a (self, värde, platser):
self.värde = r_fel(self.värde)
lista =[]
for s in k_fel(värde, platser):
#yield(s)
lista += s
return lista

värde =[]
värde = range(1,10)
antal = 4
for i in alla_värden(värde, antal):
print (i)
``````

When i run this i get that the class is not iterable. how do i pass around that?

Your use of antal is strange, you save it as self.antal, but after that you seem to not refer it anywhere. What kind of result you would expect from alla_värden? Usually we use CapitalizedName for classes to separate them to instance variables. So AllaVärden, and you would make an instance from it before loop and iterate over the instance. BTW in my Python at least he scandinavian letters are not allowed for names of Python objects.

BTW in my Python at least he scandinavian letters are not allowed for names of Python objects.

In my python 3.3.1, french accented letters are allowed, as in

``````>>> incrément = 1
>>> hôte = '127.0.0.1'
>>>
``````

Another Python3 vs Python 2 difference, maybe?

I think so.

OK AllaVärden is supposed to make an object that contains lists of all the variables. If you run the main code you will get all list, they consist of all different combinations of 4 digits using 1-9.

I need to make the same thing but using a class instead.

using python 3

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.