W dniu 17.10.2016 o 18:16, Chris Angelico pisze:
On Tue, Oct 18, 2016 at 3:03 AM, Mr. Wrobel <m...@e-wrobel.pl> wrote:
Hi,

I am looking for an example of metaclass usage. Especially I am interestet
in manipulating instance variables, for example:

My class:
class MrMeta(type):
        pass

class Mr(object):
        __metaclass__ = MrMeta

        def __init__(self):
                self.imvariable = 'Zmienna self'

        def aome method(self):
                print 'I am in instance'

So in general, I would like to create a mechanism, that is changing value
for self.imvariable to capital letters and this should be included in my
metaclass called MrMeta(type).

Can you guide me please?

Are you sure you can't just use a descriptor, such as @property?

class Mr(object):
    @property
    def imvariable(self):
        return self._imvariable
    @imvariable.setter
    def imvariable(self, value):
        self._imvariable = str(value).upper()

ChrisA

Hi,

I am sure that I can do that with setter/getter, but I want to be closer to black magic, that is why I wanted to inlcude Metaclasses.

I know how to acomplish that for class variables, but don;t have any idea how to instance.

Mr. Wrobel
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to