473,395 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to use a parameter in a class

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?
Jun 27 '08 #1
4 1122
On May 3, 1:05*pm, Decebal <CLDWester...@gmail.comwrote:
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):
...
Jun 27 '08 #2
On Sat, 03 May 2008 03:05:31 -0700, Decebal wrote:
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

--
Ivan
Jun 27 '08 #3
Decebal wrote:
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.
>

#####

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?
--
http://mail.python.org/mailman/listinfo/python-list
Jun 27 '08 #4
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
On Sat, May 3, 2008 at 12:05 PM, Decebal <CL**********@gmail.comwrote:
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?
--
http://mail.python.org/mailman/listinfo/python-list
Jun 27 '08 #5

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

Similar topics

3
by: Mysooru | last post by:
Hi All, One of the ATL class..... template <class Base> class CComObject : public Base { public: typedef Base _BaseClass;
3
by: Dave | last post by:
Hello all, Quoting from page 24 of "The Boost Graph Library; User Guide and Reference Manual": "It turns out that by the contravariance subtyping rule, the parameter type in the derived...
8
by: Tony Johansson | last post by:
Hello Experts! What does this mean actually. If you have a template with a type and non-type template argument, say, like this template<typename T, int a> class Array {. . .}; then A<int,...
2
by: Siegfried Weiss | last post by:
Hi guys, i give up finding a solution by reading or by trial & error. Hope, YOU can help me! (Sorry for my rather long posting.) Stroustrup says, that templates could be declared with - type...
4
by: Ondrej Spanel | last post by:
The code below does not compile with .NET 2003, I get folowing error: w:\c\Pokusy\delegTemplArg\delegTemplArg.cpp(11) : error C2993: 'float' : illegal type for non-type template parameter 'x' ...
7
by: Richard Grant | last post by:
Hi. In c/C++ i can pass the address of a subroutine to another subroutine as an actual parameter How do I do that in VB .NET What should be the syntax for a parameter to receive the address of a...
5
by: Ram | last post by:
Hi Friends I want to develope a custom control in .net which can be used with any project. I am writing a function in that class which I want to take any object as parameter. For that I have...
3
by: Ross McLean | last post by:
Hi all, I've been teaching myself C# for a new project at work. I have a bit of a background in c++ and java but never been what you could call a guru. I'm having some strange things happening...
4
by: David Sanders | last post by:
Hi, I have a class with an integer template parameter, taking values 1, 2 or 3, and a function 'calc' in that class which performs calculations. Some calculations need only be performed if the...
4
by: abir | last post by:
I am matching a template, and specializing based of a template, rather than a single class. The codes are like, template<template<typename T,typename Alloc = std::allocator<T> class pix{ }; ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.