static? + some stuff

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Schüle

    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_a ll_attr_of_clas s(cls):
    tmp = cls()
    attr = dir(cls) #or dir(tmp)
    print attr
    for i in attr:None
    #print type(tmp.i)

    print_type_of_a ll_attr_of_clas s(foo)


  • Daniel Schüle

    #2
    Re: static? + some stuff

    [...]
    [color=blue]
    > a, b, c and my_id *are* class attributes. They are actually inherited by[/color]

    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)
    [color=blue]
    > instances of foo, but declaring them this way makes them class attributes.[/color]
    You[color=blue]
    > 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
    > (...)[/color]

    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?
    [color=blue]
    > Attributes do not need to be *declared* at the top of the class in Python[/color]
    as in[color=blue]
    > C++ or Java: initializing them in the __init__ method is enough, and is[/color]
    probably[color=blue]
    > the safest way to handle them.[/color]

    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


    [...]
    [color=blue]
    > for i in attr:
    > print type(getattr(tm p, i))[/color]

    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
    ;)


    Comment

    • Alex Martelli

      #3
      Re: static? + some stuff

      Daniel Schüle wrote:
      [color=blue]
      > [...]
      >[color=green]
      >> a, b, c and my_id *are* class attributes. They are actually inherited by[/color]
      >
      > 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)[/color]

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

      [color=blue]
      > from you code above i dont see the declarations of c, my_id ...[/color]

      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

      Comment

      • Daniel Schüle

        #4
        Re: static? + some stuff

        [...]
        [color=blue]
        > 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[/color]

        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!!!

        [...]


        Comment

        Working...