I want to call the mother class but I get this message :
Traceback (most recent call last):
File "***test.py", line 23, in <module>
for i in daughter:
File "***test.py", line 18, in __iter__
for i in super(Mother, self):
TypeError: 'super' object is not iterable
I think it's just about the syntax, I try to call super(Mother, self) without any method, just the object itself.
Here the code :
class Mother(object):
def __init__(self, upperBound):
self.upperBound = upperBound
def __iter__(self):
for i in range (self.upperBound):
yield i
class Daughter(Mother):
def __init__(self, multiplier, upperBound):
self.multiplier = multiplier
super(Daughter, self).__init__(upperBound)
def __iter__(self):
for i in super(Mother, self): # Here
yield i * self.multiplier
daughter = Daughter(2, 4)
for i in daughter:
print i
Here it's just an exemple, my purpose is to read a file and yield line by line. Then a subclass generator parse all lines (for exemple make a list from the line...).
The proxy object returned by super() is not iterable just because there is an __iter__ method in the MRO. You need to look up such methods explicitly, as only that'll kick of a search:
for i in super(Daughter, self).__iter__():
yield i * self.multiplier
Note that you need to use super() on the current class, not the parent.
super() can't directly support special methods because these are looked up directly on the type by Python, not the instance. See Special method lookup for new-style classes:
For new-style classes, implicit invocations of special methods are only guaranteed to work correctly if defined on an object’s type, not in the object’s instance dictionary.
type(super(Daughter, self)) is the super type object itself, and it doesn't have any special methods.
Demo:
>>> class Mother(object):
... def __init__(self, upperBound):
... self.upperBound = upperBound
... def __iter__(self):
... for i in range (self.upperBound):
... yield i
...
>>> class Daughter(Mother):
... def __init__(self, multiplier, upperBound):
... self.multiplier = multiplier
... super(Daughter, self).__init__(upperBound)
... def __iter__(self):
... for i in super(Daughter, self).__iter__():
... yield i * self.multiplier
...
>>> daughter = Daughter(2, 4)
>>> for i in daughter:
... print i
...
0
2
4
6
Related
I have the following error:
Error Traceback (most recent call last): line 25,line 14,line 3 AttributeError: 'str' object has no attribute 'Teamname'
Here is my python code:
class Team:
def __init__(self,Name = "Name",Origin= "india"):
self.Teamname = Name
self.Teamorigin= Origin
def DefTeamname(self,Name):
self.Teamname = Name
def defTeamorigin(self,Origin):
self.Teamorigin = Origin
class Player(Team):
def __init__(self,Pname,Ppoints,Tname,Torigin):
Team.__init__(Tname,Torigin)
self.Playername = Pname
self.Playerpoints = Ppoints
def Scoredpoint(self):
self.Playerpoints += 1
def __str__(self):
return self.Playername + "has scored" + str(self.Playerpoints) + "points"
Player1 = Player('Sid',0,'Gokulam','Kochi')
print(Player1)
What am I doing wrong?
Your error is being thrown because the first argument to __init__ is being interpreted as self, and __init__ is therefore trying to mutate it. It only works at all because you use keyword arguments, so Origin is just falling back to the default.
Note for completeness that you can call the method directly on the target class (or any other class for that matter), but that self is not bound and you lose the advantage of super() always pointing to the parent in the MRO. This works:
class A:
def __init__(self, a):
self._a = a
class B:
def __init__(self, a, b):
self._b = b
A.__init__(self, a)
b = B(6, 7)
assert b._a == 6
Incidentally this shows that __init__ is just a function which takes a muteable first arg (self by convention) and mutates that arg.
You really should use super() however. What happens if I redefine A?:
class newA:
def __init__(self):
self._other = True
class A(newA):
...
If you have used super() all the way through, everything will work fine:
class NewA:
def __init__(self, **kwargs):
super().__init__(**kwargs)
class A(NewA):
def __init__(self, a=None, **kwargs):
self._a = a
super().__init__(**kwargs)
Note the use of keyword arguments to pass up the chain without worrying about the semantics of every class's init.
Further Reading
Python's super considered super
Python's super considered harmful for warnings about how things can go wrong if you don't keep your semantics compatible.
class Player(Team):
def __init__(self,Pname,Ppoints,Tname,Torigin):
super().__init__(Tname,Torigin)
...
You cannot call __init__ yourself on the class, you need to call on the instance by using super() notation, else the self parameter will not be bound correctly
Let's say I have an Entity class:
class Entity(dict):
pass
def save(self):
...
I can wrap a dict object with Entity(dict_obj)
But is it possible to create a class that can wrap any type of objects, eg. int, list etc.
PS I have come up the following work around, it doesn't work on the more complex objects, but seems to work with basic ones, completely unsure if there are any gotchas, might get penalised with efficiency by creating the class every time, please let me know:
class EntityMixin(object):
def save(self):
...
def get_entity(obj):
class Entity(obj.__class__, EntityMixin):
pass
return Entity(obj)
Usage:
>>> a = get_entity(1)
>>> a + 1
2
>>> b = get_entity('b')
>>> b.upper()
'B'
>>> c = get_entity([1,2])
>>> len(c)
2
>>> d = get_entity({'a':1})
>>> d['a']
1
>>> d = get_entity(map(lambda x : x, [1,2]))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/jlin/projects/django-rest-framework-queryset/rest_framework_queryset/entity.py", line 11, in get_entity
return Entity(obj)
TypeError: map() must have at least two arguments.
Improve efficiency:
EntityClsCache = {}
class EntityMixin(object):
def save(self):
...
def _get_entity_cls(obj):
class Entity(obj.__class__, EntityMixin):
pass
return Entity
def get_entity(obj)
cls = None
try:
cls = EntityClsCache[obj.__class__]
except AttributeError:
cls = _get_entity_cls(obj)
EntityClsCache[obj.__class__] = cls
return cls(obj)
The solution you propose looks elegant, but it lacks caching, as in, you'll construct a unique class every time get_entity() is called, even if types are all the same.
Python has metaclasses, which act as class factories. Given that metaclass' methods override these of class, not the instance, we can implement class caching:
class EntityMixin(object):
pass
class CachingFactory(type):
__registry__ = {}
# Instead of declaring an inner class,
# we can also return type("Wrapper", (type_, EntityMixin), {}) right away,
# which, however, looks more obscure
def __makeclass(cls, type_):
class Wrapper(type_, EntityMixin):
pass
return Wrapper
# This is the simplest form of caching; for more realistic and less error-prone example,
# better use a more unique/complex key, for example, tuple of `value`'s ancestors --
# you can obtain them via type(value).__mro__
def __call__(cls, value):
t = type(value)
typename = t.__name__
if typename not in cls.__registry__:
cls.__registry__[typename] = cls.__makeclass(t)
return cls.__registry__[typename](value)
class Factory(object):
__metaclass__ = CachingFactory
This way, Factory(1) performs Factory.__call__(1), which is CachingFactory.__call__(1) (without metaclass, that'd be a constructor call instead, which would result in a class instance -- but we want to make a class first and only then instantiate it).
We can ensure that the objects created by Factory are the instances of the same class, which is crafted specifically for them at the first time:
>>> type(Factory(map(lambda x: x, [1, 2]))) is type(Factory([1]))
True
>>> type(Factory("a")) is type(Factory("abc"))
True
I would like to create a class which returns an int when initiated, like so:
r = Foo(10)
print r # 1000
I know you can do this by overriding the __new__ method. However I need it to also execute other class functions within the __new__ method, how do I do this?
So far I have:
class Foo(object):
def __new__(cls, i):
cls.i = i
return cls.foo_fun()
def foo_fun(self):
return self.i * 100
print Foo(5)
and the error I get:
Traceback (most recent call last):
return cls.foo_fun()
TypeError: unbound method foo_fun() must be called with Foo instance as first argument (got nothing instead)
You don't have an instance in your __new__ factory method (which is static, really). You don't have a self to call things on. Use another static or class method:
class Foo(object):
def __new__(cls, i):
return cls.foo_fun(i)
#staticmethod
def foo_fun(i):
return i * 100
print Foo(5)
Setting cls.i is not thread-safe as that state is shared between all __new__ calls; you are much better off passing along the value as a parameter to another method.
However, you are abusing classes here; you never create an instance of this class, there is no way to use the class in isinstance() type checks, etc. Just use a factory function:
def foo(i):
return i * 100
If you really meant for this to be a subclass of int, you'll still need to create an actual instance of your class to return:
class Foo(int):
def __new__(cls, i):
i = int(i) # ensure you have an actual integer first
value = cls.foo_fun(i)
return super(Foo, cls).__new__(cls, value)
#staticmethod
def foo_fun(i):
return i * 100
The above inherits from int, handles the case where the argument is not an integer (like"42"`, a string convertible to an integer) and returns an instance of your class.
Demo:
>>> class Foo(int):
... def __new__(cls, i):
... i = int(i) # ensure you have an actual integer first
... value = cls.foo_fun(i)
... return super(Foo, cls).__new__(cls, value)
... #staticmethod
... def foo_fun(i):
... return i * 100
...
>>> f = Foo(42)
>>> f
4200
>>> isinstance(f, Foo)
True
>>> Foo("42") # non-integer input works too
4200
I know the questions about: copy properties, or dynamic creation of properties has already been posted and also been answered (here, here and here). You could also find an excellent description, how the property function works here.
But I think, that my question is a bit more specific. I do not only want to copy the property from one class to another. No, I also want the specific getter, setter and deleter functions to be copied to the destination class. After a whole day of searching for an answer, I decided to create an new post for this question.
So let me get a bit more in detail. A have an attribute class which is more a class group and stores property-classes:
class AttrContainer():
class a():
ATTR=1
#property
def a(self):
return self.ATTR
#a.setter
def a(self, n):
self.ATTR = n + 3.021
class b():
ATTR=None
#property
def b(self):
return "Something"
class c():
ATTR=None
#property
def c(self):
return 3
#c.setter
def c(self, n):
self.ATTR = n - 8.5201
As you can see, I have different getter, setter (not in the example: deleter) definitions of each property.
I want to use those properties with my item "wrapper" objects. But not all of item objects needs all properties, thats why I want to copy them dynamically into my wrapper classes.
So, this is how my item "wrapper" classes looks like:
class Item01Object():
properties = ["a","c"]
ATTR = None
#[...]
class Item02Object():
properties = ["b","c"]
ATTR = None
#[...]
#[...]
Because I can't set the properties dynamically while the item class will be instanced, I have to set them before I instance the class:
def SetProperties( ItemObject ):
for propName, cls in AttrContainer.__dict__.iteritems():
if propName in ItemObject.properties:
prop = cls.__dict__[propName]
fget = prop.fget if prop.fget else None
fset = prop.fset if prop.fset else None
fdel = prop.fdel if prop.fdel else None
ItemObject.__dict__[propName] = property(fget,fset,fdel)
return ItemObject()
In the end, i would instance my ItemObjects like this:
item = SetProperties(Item01Object)
I would expect, that this will work...
>>> print item
<__builtin__.Item01Object instance at 0x0000000003270F88>
>>> print item.a
None
This is result is right, because I do not update my property ATTR..
Lets change the property:
>>> item.a = 20
>>> print item.a
20
But this result is wrong, it should be 23.021 and NOT 20 . It looks like my properties do not using the setter functions from its classes.
Why? What do I wrong in my code?
Edit: Sorry, I forgot to remove the inherited object of the ItemObject classes.. Now the code works.
For properties with setters and deleters to work properly, your classes need to inherit from object: Why does #foo.setter in Python not work for me?
You can just copy the property object itself over to the new class. It'll hold references to the getter, setter and deleter functions and there is no need to copy those across.
For new-style classes, your code is not working; you cannot assign to a class __dict__ attribute:
>>> item = SetProperties(Item01Object)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in SetProperties
TypeError: 'dictproxy' object does not support item assignment
Use setattr() instead to set attributes on new-style classes:
def SetProperties( ItemObject ):
for propName, cls in AttrContainer.__dict__.iteritems():
if propName in ItemObject.properties:
setattr(ItemObject, propName, cls.__dict__[propName])
return ItemObject()
Note that the property object is copied across wholesale.
Demo:
>>> class Item01Object(object):
... properties = ["a","c"]
... ATTR = None
...
>>> def SetProperties( ItemObject ):
... for propName, cls in AttrContainer.__dict__.iteritems():
... if propName in ItemObject.properties:
... setattr(ItemObject, propName, cls.__dict__[propName])
... return ItemObject()
...
>>> item = SetProperties(Item01Object)
>>> item
<__main__.Item01Object object at 0x108205850>
>>> item.a
>>> item.a = 20
>>> item.a
23.021
You only have to copy across property objects to the target class once though; that your function returns an instance implies you are planning to use it for all instances created.
I'd make it a decorator instead:
def set_properties(cls):
for name, propcls in vars(AttrContainer).iteritems():
if name in cls.properties:
setattr(cls, name, vars(propcls)[name])
return cls
then use this on each of your Item*Object classes:
#set_properties
class Item01Object(object):
properties = ["a","c"]
ATTR = None
#set_properties
class Item02Object(object):
properties = ["b","c"]
ATTR = None
Demo:
>>> def set_properties(cls):
... for name, propcls in vars(AttrContainer).iteritems():
... if name in cls.properties:
... setattr(cls, name, vars(propcls)[name])
... return cls
...
>>> #set_properties
... class Item01Object(object):
... properties = ["a","c"]
... ATTR = None
...
>>> #set_properties
... class Item02Object(object):
... properties = ["b","c"]
... ATTR = None
...
>>> item01 = Item01Object()
>>> item01.c = 20
>>> item01.c
3
>>> item02 = Item02Object()
>>> item02.b = 42
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: can't set attribute
>>> item02.b
'Something'
I have a method (__init__) in a class, and I want to use a function from the class in this method.
But when I want to run my program. I get: NameError: global name 'myfunction' is not defined
Someone, who knows what I have to do? :)
Thank you. But I have still a problem, because def myFunc(self, a): is a method and I wanted a function.
class Myclass(object):
def __init__(self, a):
self.a = self.myFunc(a)
def myFunc(self, a):
return a+1
Then you don't have a function call in the method, but you have a method call in it.
When creating a class you must specify the object when calling its methods:
>>> class A(object):
... def __init__(self, val):
... self.val = self._process(val)
... def _process(self, val):
... return val % 7
... process = _process #if you are outside methods then you don't
... #have to add "self.".
...
>>> a = A(5)
>>> a.process(3)
3
>>> a._process(6) #"a" is passed as the "self" parameter
6
As you can see in a class definition, but outside the methods you must specify the method name only, and not the "self.". Also you can't refer to a method not already defined:
>>> class B(object):
... def __init__(self):pass
... def method1(self):pass
... __call__ = method2 #method2 not defined!
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in B
NameError: name 'method2' is not defined