How to use a parameter in a class

D

Decebal

I have the following class:
#####
class Dummy():
value = 0
def __init__(self, thisValue):
print thisValue
self.value = thisValue
value = thisValue

def testing(self):
print 'In test: %d' % self.value

def returnValue(self):
return self.value

result = someFuntion(default = value)
#####

But the last line does not work.
I would like to do a call like:
dummy = Dummy(thisValue = 12)

And that someFunction gets a default value of 12. How can I do that?
 
F

Florian Herlings

Hello Decebal,

I am new to python myself, which might be the cause why I do not get
that "last line" at all. To me it looks like you are trying to set a
variable in the class body (if I am reading the indent correctly) and
call a function (that does not exist?) to calculate the value for it.
Does that work at all?

About a week ago I learned something, that might solve your problem:
You can only access the instance's variables from within a function.
The variable is not visible from the outside. To get that value (12 on
your example) you have to be in a function, or call your returnValue()
function from the outside.

I hope, that I did not waste your time with my lowest level knowledge.

Have a nice weekend,
Florian
 
S

svetoslav.agafonkin

I have the following class:
#####
class Dummy():
    value = 0
    def __init__(self, thisValue):
        print thisValue
        self.value = thisValue
        value = thisValue

    def testing(self):
        print 'In test: %d' % self.value

    def returnValue(self):
        return self.value

    result = someFuntion(default = value)
#####

But the last line does not work.
I would like to do a call like:
    dummy = Dummy(thisValue = 12)

And that someFunction gets a default value of 12. How can I do that?

The line
result = someFuntion(default = value)
is executed when the whole 'class' compound statement is executed,
i.e.
before the line that creates the 'dummy' instance. Moreover, this
happens only once and not every time you create a new instance of
the class. If you want 'result' to be a class attribute that is set
every time you create a new instance move the line
result = someFuntion(default = value)
in the __init__ constructor (you also have to assign some value to it
before,
just like the 'value' attribute and preffix it with Dummy. otherwise
it'll
be considered as local name of __init__):

class Dummy():
value = 0
result = 0

def __init__(self, thisValue):
print thisValue
self.value = thisValue
Dummy.value = thisValue
Dummy.result = someFuntion(default = Dummy.value)

def testing(self):
...
 
I

Ivan Illarionov

I have the following class:
#####
class Dummy():
value = 0
def __init__(self, thisValue):
print thisValue
self.value = thisValue
value = thisValue

def testing(self):
print 'In test: %d' % self.value

def returnValue(self):
return self.value

result = someFuntion(default = value)
#####

But the last line does not work.
I would like to do a call like:
dummy = Dummy(thisValue = 12)

And that someFunction gets a default value of 12. How can I do that?

class Dummy(object):
def __init__(self, thisValue):
self.value = thisValue

def someFunction(self, default=None):
if default is None:
default = self.value
 
G

Gary Herron

Decebal said:
I have the following class:
#####
class Dummy():
value = 0
def __init__(self, thisValue):
print thisValue
self.value = thisValue
value = thisValue

def testing(self):
print 'In test: %d' % self.value

def returnValue(self):
return self.value

result = someFuntion(default = value)

But of course it doesn't work, as it tries to call someFunction and no
such thing is define anywhere.

But that can't be the answer to the *real* question you are trying to
ask. Why don't you tell us what you are trying to do here, and we'll
wee if we can help.

Gary Herron


P.S. Having a class attribute AND an instance attribute, both named
"value" is going to cause trouble.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top