Re: Meta classes - real example

2016-10-20 Thread Mr. Wrobel

W dniu 18.10.2016 o 16:42, Ethan Furman pisze:

On 10/17/2016 11:44 PM, Mr. Wrobel wrote:


Ok,so in general, we could say that using Metclasses is ok for
manipulating __new__ but not that what is setting by __init__. Am I
right?


No and yes.

In this code (python 2 syntax):

#untested
class Wonderful(object):

__metaclass__ = SomeMetaClass
diamond = Sparkly(7)
club = 9

def display(self):
print "I'm an instance!"

The metaclass controls what happens when Wonderful is created:

- it can add the name 'diamond' to the Sparkly descriptor;
-  it can change/remove/add other attributes such as club, spade, or
whatever;
- it can wrap methods such as display to pre- or post-process calls to it
- etc.

Once the class (Wonderful, in this example) has been created:

- x = Wonderful()  # metaclass not called
- x.display()  # metaclass not called
- x.diamond# metaclass not called

- list(Wonderful)  # metaclass called (e.g. SomeMetaClass.__iter__(cls) )
   # where cls = Wonderful


Check out http://stackoverflow.com/a/35730545/208880 for a simple
demonstration of a metaclass.

--
~Ethan~

Thank you Ethan!
--
https://mail.python.org/mailman/listinfo/python-list


Re: Meta classes - real example

2016-10-18 Thread Ethan Furman

On 10/17/2016 11:44 PM, Mr. Wrobel wrote:


Ok,so in general, we could say that using Metclasses is ok for manipulating 
__new__ but not that what is setting by __init__. Am I right?


No and yes.

In this code (python 2 syntax):

#untested
class Wonderful(object):

__metaclass__ = SomeMetaClass

diamond = Sparkly(7)

club = 9

def display(self):
print "I'm an instance!"

The metaclass controls what happens when Wonderful is created:

- it can add the name 'diamond' to the Sparkly descriptor;
-  it can change/remove/add other attributes such as club, spade, or whatever;
- it can wrap methods such as display to pre- or post-process calls to it
- etc.

Once the class (Wonderful, in this example) has been created:

- x = Wonderful()  # metaclass not called
- x.display()  # metaclass not called
- x.diamond# metaclass not called

- list(Wonderful)  # metaclass called (e.g. SomeMetaClass.__iter__(cls) )
   # where cls = Wonderful


Check out http://stackoverflow.com/a/35730545/208880 for a simple demonstration 
of a metaclass.

--
~Ethan~
--
https://mail.python.org/mailman/listinfo/python-list


Re: Meta classes - real example

2016-10-18 Thread Mr. Wrobel

W dniu 17.10.2016 o 23:23, Ethan Furman pisze:

On 10/17/2016 09:23 AM, Mr. Wrobel wrote:

W dniu 17.10.2016 o 18:16, Chris Angelico pisze:

On Tue, Oct 18, 2016 at 3:03 AM, Mr. Wrobel wrote:



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


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.


Metaclasses work on the class level, not the instance level.  The only*
influnce a metaclass is going to have on instances is changes it makes
while it's creating the class.

For example, MrMeta could set descriptors in Mr when it is creating the
Mr class; it could even put it's own __init__ in the class; but to
affect things like instance variables that are added in methods -- well,
it is possible, but it is *a lot* of work.

--
~Ethan~

* Okay, somebody prove me wrong!  :)


Ok,so in general, we could say that using Metclasses is ok for 
manipulating __new__ but not that what is setting by __init__. Am I right?


MrWrobel
--
https://mail.python.org/mailman/listinfo/python-list


Re: Meta classes - real example

2016-10-17 Thread Ethan Furman

On 10/17/2016 09:23 AM, Mr. Wrobel wrote:

W dniu 17.10.2016 o 18:16, Chris Angelico pisze:

On Tue, Oct 18, 2016 at 3:03 AM, Mr. Wrobel wrote:



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


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.


Metaclasses work on the class level, not the instance level.  The only* 
influnce a metaclass is going to have on instances is changes it makes while 
it's creating the class.

For example, MrMeta could set descriptors in Mr when it is creating the Mr 
class; it could even put it's own __init__ in the class; but to affect things 
like instance variables that are added in methods -- well, it is possible, but 
it is *a lot* of work.

--
~Ethan~

* Okay, somebody prove me wrong!  :)
--
https://mail.python.org/mailman/listinfo/python-list


Re: Meta classes - real example

2016-10-17 Thread Mr. Wrobel

W dniu 17.10.2016 o 18:16, Chris Angelico pisze:

On Tue, Oct 18, 2016 at 3:03 AM, Mr. Wrobel  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


Re: Meta classes - real example

2016-10-17 Thread Chris Angelico
On Tue, Oct 18, 2016 at 3:03 AM, Mr. Wrobel  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
-- 
https://mail.python.org/mailman/listinfo/python-list