473,325 Members | 2,785 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,325 software developers and data experts.

static? + some stuff

Hi all

I have 2 questions
1)
is there a way do declare static variables in a class?
I coded this logic in global variable foo_cnt
2)
is there a way to see the types of all data/function members of a class?
dir(cls) returns a list of strings (of members) so it's logical that
type(tmp.i) fails ...
is it possible at all?

thanks for your time

--
Daniel
foo_cnt = 0 #<<<<<replace for static

class foo:
a = b = None
c = 0
my_id = 0
def __init__(self, c = 0):
global foo_cnt
foo_cnt+=1
self.my_id = foo_cnt
self.c = c
def show(self):
print self.a, self.b, self.c
def move(self):
print "ID = ", self.my_id, "\tc = ", self.c

print dir(foo) #shows all member of 'foo'

x = foo(12)
y = foo(123)

print foo_cnt
x.show()
y.show()

def print_type_of_all_attr_of_class(cls):
tmp = cls()
attr = dir(cls) #or dir(tmp)
print attr
for i in attr:None
#print type(tmp.i)

print_type_of_all_attr_of_class(foo)
Jul 18 '05 #1
3 1619
[...]
a, b, c and my_id *are* class attributes. They are actually inherited by
just to adjust myself to the Python's terminology
is the word "member" not widely used in Python?
is it better to say "attributes"?
(for both member functions and member variables)
instances of foo, but declaring them this way makes them class attributes. You should have done:

class foo:
cnt = 0
def __init__(self, c=0):
foo.cnt += 1
self.a = self.b = None
self.c = c
self.my_id = foo.cnt
(...)
i think i've got it
the only thing bothering me is that you cant see(less readability) from
...
cnt = 0
...
is "cnt" considered to be used as "static" or not
what if i would write ...
foo.cnt += 1 #static
and later in one of member functions
self.cnt = 1 #non static

or issues such usage an error ..

and one more question :)
from you code above i dont see the declarations of c, my_id ...
is it sufficient just to write self.c=0 instead of doing it the way i did in
my original
posting (where i put them additional at the top for the readability)?
but do the both ways express logical the same thing?
Attributes do not need to be *declared* at the top of the class in Python as in C++ or Java: initializing them in the __init__ method is enough, and is probably the safest way to handle them.
as I figured out from tests, one has to initalize every variable with
something
at least with None (and that's fine so)
is there a need to differentiate between assignment and initialization?
(like in C++)
btw in C++ you can also declare member variables at the bottom
indeed that's my favorite style there, but since i am new to Python
i found it more readable (dont ask me why) to place them at the top

i am sure there are must be some "coding styles" in use around ..
pointers in this direction are very approciated
[...]
for i in attr:
print type(getattr(tmp, i))


exactly this :)

Thank you and all others for the answears

--
Daniel

ps: i hope my english is understandable, if not .. just ask me what i ment
;)
Jul 18 '05 #2
Daniel Schüle wrote:
[...]
a, b, c and my_id *are* class attributes. They are actually inherited by
just to adjust myself to the Python's terminology
is the word "member" not widely used in Python?
is it better to say "attributes"?
(for both member functions and member variables)


Yes, for example the built-in function for accessing, checking etc
are named getattr, hasattr, setattr, delattr

from you code above i dont see the declarations of c, my_id ...


No declarations: a name springs into existence when you first assign to it
(or otherwise bind it, e.g. with a 'def name():...' statement).

It's most readable if you assign all of an instance's attributes
in the __init__ method -- it's not mandatory, but it's what is most
normally done and expected, whence the readability "from habit".
Alex

Jul 18 '05 #3
[...]
is "cnt" considered to be used as "static" or not
what if i would write ...
foo.cnt += 1 #static
and later in one of member functions
self.cnt = 1 #non static


after long thinking i came to the conclusion that .. from what KefX said ..
"assigning to it .. would smash 'static' for this instance of the class"
i would say ...

class some_class:
cnt = 0
def __init__(self):
foo.cnt += 1
self.my_id = foo.cnt
def some_func(self):
self.cnt = 100
def print(self):
print foo.cnt

x = some_class()
x.print() #1
y = some_class()
y.print() #2

x.some_func() #<--- smashes cnt ONLY for x instance?!?!
x.print() #100

z = some_class() #..but not for the instances that might come
z.print() #3

i haven't tested that, ALL OUTPUT assumed!!!

[...]
Jul 18 '05 #4

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

Similar topics

2
by: Thomas Mlynarczyk | last post by:
Hello, Is there a way to access a static variable defined inside a function from outside that function? Something like the following: function foo() { static $bar = 'stuff'; // more code }
1
by: baylor | last post by:
In C#, an interface cannot mark any method as static. i'm told the ILASM supports it but i've never tested that Two questions. First, why? OK, i've heard the reason about interfaces being...
3
by: Steven T. Hatton | last post by:
Sorry about the big code dump. I tried to get it down to the minimum required to demonstrate the problem. Although this is all done with GNU, I believe the problem I'm having may be more general. ...
13
by: Alex Vinokur | last post by:
I have the following problem. class Base { public: void (*pfunc) (int) = 0; }; class Derived : public Base {
1
by: Jeff | last post by:
Hello everybody, I have a question concerning function declarations. When exactly do you have to declare functions if you want to use them? I have two functions main() and foo(), respectively...
33
by: Chris Capel | last post by:
What is the rationale behind the decision not to allow abstract static class members? It doesn't seem like it's a logically contradictory concept, or that the implementation would be difficult or...
6
by: GG | last post by:
Is this public static method thread safe. //Receives a file name as a parameter //and returns the contents of that file as a string public static string FileToStr(string fileName) { FileStream...
1
by: Ray Ackley | last post by:
I'm experiencing a threading problem that's really perplexing me. I have a multithreaded application that reads car ECU information that's sent over the serial port by the ECU and received by the...
11
by: comp.lang.php | last post by:
function blah($item) { if (!isset($baseDir)) { static $baseDir = ''; $baseDir = $item; print_r("baseDir = $baseDir\n"); } $dirID = opendir($item); while (($fyl = readdir($dirID)) !== false)...
5
by: Andy B | last post by:
I have a class that I want to make static but it uses some objects that are instance objects. I keep getting a compiler error saying something about using instance objects in a static class or...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.