Suppose I am creating a class representing and object in a KV-datastore. Creating the constructor is pretty straightforward...
class KVObject:
def __init__(self, key, value, add=True):
self.key = key
self.value = value
if add == True:
kv_create(key, value)
Now, let's say I want to retrieve a KV-object... and construct a class instance from it. I figure I'll add this class method onto my DataBaseObject class.
#classmethod
def get_kv_object(cls, key):
value = kv_get(key)
cls.__init__(key, value, add=False)
... but when calling init from the class method (cls.__init__), Python asks for an argument for self!
Any ideas? Many thanks! I know this is a bit of a simple example, but it definitely applies to some more complex, interesting situations!
Edit: Thanks for the responses. I learned something helpful while researching this, which is that object.__new__ calls __init__ on an object when calling MyClass(...), effectively providing an uninitialized instance of the class to MyClass.init with the args called from within MyClass. Likewise, it makes sense to call cls(...), as suggested in your answers. Thank you.
Think about how you would create a new database object normally, it would be something like the following:
instance = DatabaseObject(key, value, add=False)
Well within your class method the first parameter (cls in this case) is just another name for DatabaseObject, so to create an instance you would do the following:
instance = cls(key, value, add=False)
#classmethod
def get_kv_object(cls, key):
value = kv_get(key)
return cls(key, value, add=False)
__init__ does not created new instances of the cls class! It's a method that's called right after the object has been created.
Related
Can I get self from class function? For example:
class abc:
def __init__(self, name):
self.name = name
def printname(self):
print(self.name)
x = abc("test").printname()
I want to get original class obj abc("test") from x without using return self or define x = abc("test") at first, is it possible?
The short answer is no.
The longer answer:
printname is not a "class function", it's an instance method. A class method is one that takes the class as the first parameter. Most methods are instance methods; they take self (a particular instance of the class) as the first parameter. If this were indeed a class method, there would be no "self" associated with it.
The thing you're referring to as a "class object" is an instance of the class abc. abc is itself an object (everything in Python is an object, including class definitions), but that's not the same thing as the instance you get by calling abc("test").
Any time you want to take a value that's local to a particular function call and make it available to the caller (including its parameters), you need to return it. There are technically other tricks involving mutating state in the caller's scope, but those aren't applicable to your example. If you ended printname with the line return self, then doing x = abc("test").printname() would result in the instance being assigned to x.
In your example, the caller is the one that constructs the instance, so simply writing this would also do the trick:
x = abc("test")
x.printname()
Hello i want to create a function which creates instances of a class
def make_instance(name_instance)
name_instance=puppy()
class puppy:
def __init__(self,name):
self.name =name
make_instance(cloud)
# when i pass an argument it says the variable is undefined and i use #isinstance() it return False.
Your puppy class needs to take a name value into its constructor, and you're currently not passing in anything.
Also your function doesn't return the instance at all. It simply re-assigns the instance to the variable name_instance that you pass in (losing your input). The return value of make_instance right now is None
My guess is that you want your implementation to look like the following
def make_instance(name_instance)
return puppy(name_instance)
I do want to point out though that this function isn't useful unless it does more than just create the instance, you're just adding wrapper code around the constructor
I have class with custom getter, so I have situations when I need to use my custom getter, and situations when I need to use default.
So consider following.
If I call method of object c in this way:
c.somePyClassProp
In that case I need to call custom getter, and getter will return int value, not Python object.
But if I call method on this way:
c.somePyClassProp.getAttributes()
In this case I need to use default setter, and first return need to be Python object, and then we need to call getAttributes method of returned python object (from c.somePyClassProp).
Note that somePyClassProp is actually property of class which is another Python class instance.
So, is there any way in Python on which we can know whether some other methods will be called after first method call?
No. c.someMethod is a self-contained expression; its evaluation cannot be influenced by the context in which the result will be used. If it were possible to achieve what you want, this would be the result:
x = c.someMethod
c.someMethod.getAttributes() # Works!
x.getAttributes() # AttributeError!
This would be confusing as hell.
Don't try to make c.someMethod behave differently depending on what will be done with it, and if possible, don't make c.someMethod a method call at all. People will expect c.someMethod to return a bound method object that can then be called to execute the method; just define the method the usual way and call it with c.someMethod().
You don't want to return different values based on which attribute is accessed next, you want to return an int-like object that also has the required attribute on it. To do this, we create a subclass of int that has a getAttributes() method. An instance of this class, of course, needs to know what object it is "bound" to, that is, what object its getAttributes() method should refer to, so we'll add this to the constructor.
class bound_int(int):
def __new__(cls, value, obj):
val = int.__new__(cls, value)
val.obj = obj
return val
def getAttributes(self):
return self.obj.somePyClassProp
Now in your getter for c.somePyClassProp, instead of returning an integer, you return a bound_int and pass it a reference to the object its getAttributes() method needs to know about (here I'll just have it refer to self, the object it's being returned from):
#property
def somePyClassProp(self):
return bound_int(42, self)
This way, if you use c.somePyPclassProp as an int, it acts just like any other int, because it is one, but if you want to further call getAttributes() on it, you can do that, too. It's the same value in both cases; it just has been built to fulfill both purposes. This approach can be adapted to pretty much any problem of this type.
It looks like you want two ways to get the property depending on what you want to do with it. I don't think there's any inherent Pythonic way to implement this, and you therefore need to store a variable or property name for each case. Maybe:
c.somePyClassProp
can be used in the __get__ and
c.somePyClassProp__getAttributes()
can be implemented in a more custom way inside the __getattribute__ function.
One way I've used (which is probably not the best) is to check for that exact variable name:
def __getattribute__(self, var_name):
if ('__' in var_name):
var_name, method = var_name.split('__')
return object.__getattribute__(self, var_name).__getattribute__(method)
Using object.__get__(self, var_name) uses the object class's method of getting a property directly.
You can store the contained python object as a variable and the create getters via the #property dectorator for whatever values you want. When you want to read the int, reference the property. When you want the contained object, use its variable name instead.
class SomePyClass(object):
def getInt(self):
return 1
def getAttributes(self):
return 'a b c'
class MyClass(object):
def __init__(self, py_class):
self._py_class = py_class
#property
def some_property(self):
return self._py_class.getInt()
x = MyClass(SomePyClass())
y = self.some_property
x._py_class.getAttributes()
This question may look silly(since I am new to python), but can you guys tell me what is the difference between self and classname when Binding?
class OnlyOne(object):
class __OnlyOne:
def __init__(self):
self.val = None
def __str__(self):
return ´self´ + self.val
instance = None
def __new__(cls): # __new__ always a classmethod
if not OnlyOne.instance:
OnlyOne.instance = OnlyOne.__OnlyOne()
return OnlyOne.instance
def __getattr__(self, name):
return getattr(self.instance, name)
def __setattr__(self, name):
return setattr(self.instance, name)
Here, I usually use Instance as self... What is the difference between using self and Only one... my intuition tells me that, it is a global variable.... if it is a global variable, it does not make sense at all(I will edit this, if its a global variable). Thanks!!
Ok, I think I've got a handle on your code ... The way it works is that when the constructor is called:
a = OnlyOne() #call constructor. This implicitly calls __new__
At this point, __new__ checks the class to see if an instance has been created (instance isn't None). If it hasn't been created, it creates an instance and puts it in the instance class attribute. Then the instance class attribute is returned which is then passed into your methods as self.
I think that if you actually need a singleton, then there's something fishy (lazy) about your program design. Singletons allow information to propagate throughout your program in strange ways (Imagine functions foo and bar both which create an instance of OnlyOne. Changes you make in foo show up when you call bar) -- It's somewhat akin to monkey patching.
If, after rethinking your design for a few months, you decide that you really do need a singleton, you can create some sort of factory class which is a lot more transparent...
I know that I can dynamically add an instance method to an object by doing something like:
import types
def my_method(self):
# logic of method
# ...
# instance is some instance of some class
instance.my_method = types.MethodType(my_method, instance)
Later on I can call instance.my_method() and self will be bound correctly and everything works.
Now, my question: how to do the exact same thing to obtain the behavior that decorating the new method with #property would give?
I would guess something like:
instance.my_method = types.MethodType(my_method, instance)
instance.my_method = property(instance.my_method)
But, doing that instance.my_method returns a property object.
The property descriptor objects needs to live in the class, not in the instance, to have the effect you desire. If you don't want to alter the existing class in order to avoid altering the behavior of other instances, you'll need to make a "per-instance class", e.g.:
def addprop(inst, name, method):
cls = type(inst)
if not hasattr(cls, '__perinstance'):
cls = type(cls.__name__, (cls,), {})
cls.__perinstance = True
inst.__class__ = cls
setattr(cls, name, property(method))
I'm marking these special "per-instance" classes with an attribute to avoid needlessly making multiple ones if you're doing several addprop calls on the same instance.
Note that, like for other uses of property, you need the class in play to be new-style (typically obtained by inheriting directly or indirectly from object), not the ancient legacy style (dropped in Python 3) that's assigned by default to a class without bases.
Since this question isn't asking about only adding to a spesific instance,
the following method can be used to add a property to the class, this will expose the properties to all instances of the class YMMV.
cls = type(my_instance)
cls.my_prop = property(lambda self: "hello world")
print(my_instance.my_prop)
# >>> hello world
Note: Adding another answer because I think #Alex Martelli, while correct, is achieving the desired result by creating a new class that holds the property, this answer is intended to be more direct/straightforward without abstracting whats going on into its own method.