can someone explain?

Nigel Mellor nigel.mellor at reply-to.in.signature
Mon Feb 17 12:30:02 EST 2003


OO purists might argue that your "incrementable" behaviour should be wrapped
in some kind of "counter" class : -

class Counter:
    def __init__(self, initial = 0):
        self.m_Counter = initial

    def inc(self):
        self.m_Counter += 1

    def val(self):
        return self.m_Counter

o=Counter()
print o.val()
o.inc()
print o.val()

--
Nigel Mellor
nigel.mellor at ronime.com

"Pablo" <pawelp at softsystem.pl> wrote in message
news:2ac185f4.0302170835.5d51ee28 at posting.google.com...
> Hello everyone.
>
> I'm interested in Python and I've been reading some tutorials,
> documents, etc. but could't find an answer for a few questions.
> How can I pass an address (reference) of a variable to a function?
> I know that when passing a list, its reference is actually passed. I
> assume the same applies to class's instances. But what about other
> variables?
>
> def increase(val):
>   val +=1
>
> k = 0
> increase(k)
> print k
>
> What should I do to make k equal 1 in that case? increase() changes
> only a copy, how to make it change passed variable?
>
> Why doesn't python let encapsulate attributes in classes? I can add
> attribute to class whenever I want. I don't see any reason why.
>
> And the last question.
> def f(a,L=[]):
>   L.append(a)
>   return L
>
> >>f(1)
> [1]
> >>f(2)
> [1,2]
> >>f(3)
> [1,2,3]
>
> L is a default argument so every time when a function f is invoked, L
> should be created and as a result f should return a list with only one
> value.
> Why doesn't it work that way?
>
> I really like Python, lists, dictionaries and tuples. I'm considering
> this language as a good tool for building gtk frontends but I'm really
> disapointed that the same logic from C++ or Java doesn't exist in
> Python.
> I hope someone can explain it to me.
> Thanks in advance.
>
> Cheers
> Pablo
>
> P.S. Please place pawelp at mail dot softsystem dot pl in CC field.
> Thanks a lot






More information about the Python-list mailing list