lo there all,
i have an app that runs three classes as threads in the same program.
some of them need to be able to share some functions. Can i set a
function before i define a class and have the class use it ? Kinda like
this.
def some_function(var1, var2):
do something with var1, var2
return result
class do_something1(threading.Thread):
def __init__(var):
do something
def run():
var1 = 3
var2 = 4
result = some_function(var1,var2)
is this legal ? is it pythonic?
i ask because i plan to do a big re-write soon, and want to get rid of
some repetition
thanks 7 1457
nephish:
is this legal ? is it pythonic?
It's legan and pythonic. Functions are here for a purpose.
Bye,
bearophile be************@lycos.com wrote:
nephish:
is this legal ? is it pythonic?
It's legan and pythonic. Functions are here for a purpose.
Bye,
bearophile
cool enough, and thanks for the quick reply.
shawn
nephish wrote:
be************@lycos.com wrote:
nephish:
is this legal ? is it pythonic?
It's legan and pythonic. Functions are here for a purpose.
Bye,
bearophile
cool enough, and thanks for the quick reply.
shawn
one more question.
the functions defined above the classes that the could be called from
within the classes, they do not need a 'self' declaration because they
are not part of a class, right?
sk
nephish:
one more question.
the functions defined above the classes that the could be called from
within the classes, they do not need a 'self' declaration because they
are not part of a class, right?
Class methods generally require the self as first parameter, functions
don't need the self. So your original code was wrong (sorry, I haven't
seen that before).
You can also inject functions as methods inside a class, and in such
case your function usually needs a self parameter too.
Bye,
bearophile be************@lycos.com wrote:
nephish:
one more question.
the functions defined above the classes that the could be called from
within the classes, they do not need a 'self' declaration because they
are not part of a class, right?
Class methods generally require the self as first parameter, functions
don't need the self. So your original code was wrong (sorry, I haven't
seen that before).
You can also inject functions as methods inside a class, and in such
case your function usually needs a self parameter too.
Bye,
bearophile
ok, thanks much, thats all i needed to know.
shawn
At Saturday 26/8/2006 06:13, nephish wrote:
>i have an app that runs three classes as threads in the same program. some of them need to be able to share some functions. Can i set a function before i define a class and have the class use it ? Kinda like this.
def some_function(var1, var2):
do something with var1, var2
return result
It's ok - but beware of concurrency problems. By example, two threads
trying to update the same thing. (Doesn't happen in your small example).
Gabriel Genellina
Softlab SRL
__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya! http://www.yahoo.com.ar/respuestas
At Monday 28/8/2006 14:55, shawn bright wrote:
>would it be better to move all the functions i want to share into some class and have the class threads refer to them that way ? i mean, just to keep things seperate, but resuseable ?
That really depends on your design. Python does not enforce that
*all* code be in class methods; using functions alone -as others have
pointed out- is fine.
Gabriel Genellina
Softlab SRL
__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya! http://www.yahoo.com.ar/respuestas This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Joe |
last post by:
The recipe in question is "Implementing Static Methods". It shows how to
use staticmethod(). This sentence in the Discussion section isn't clear to
me: "An attribute of a class object that...
|
by: Jerry |
last post by:
My "main" class is getting a bit long...Is it possble to split a class definition
into several files and then import the pieces to get the whole definition?
Jerry
|
by: Steve Jorgensen |
last post by:
In a recent thread, RKC (correctly, I believe), took issue with my use of
multiple parameters in a Property Let procedure to pass dimensional
arguments on the basis that, although it works, it's...
|
by: Csaba Gabor |
last post by:
I feel like it's the twilight zone here as several seemingly trivial
questions are bugging me.
The first of the following three lines is a syntax error, while the
last one is the only one that...
|
by: Bryan Parkoff |
last post by:
I write my large project in C++ source code. My C++ source code
contains approximate four thousand small functions. Most of them are
inline. I define variables and functions in the global scope....
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
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,...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| | |