Initialize attributes object class and makes it const












0















I want to create read only attribute.
The example below works well, and it is inspired and simplified from https://howto.lintel.in/how-to-create-read-only-attributes-and-restrict-setting-attribute-values-on-object-in-python/



class Attr1(object):
def __get__(self, instance, owner):
return self.value
def __set__(self, instance, value):
self.value = int(value)
class Attr2(object):
def __get__(self, instance, owner):
return self.value
def __set__(self, instance, value):
self.value = int(value)
class AttrFormula(object):
def __get__(self, instance, owner):
return instance.attr1 / instance.attr2
class Class(object):
attr1 = Attr1()
attr2 = Attr2()
attrFormula = AttrFormula()

A = Class()
A.attr1 = 1
A.attr2 = 2
print("Class A attr1 =", A.attr1)
print("Class A attr2 =", A.attr2)
print("Class A attrFormula =", A.attrFormula)
B = Class()
B.attr1 = 3
B.attr2 = 3
print("Class B attr1 =", B.attr1)
print("Class B attr2 =", B.attr2)
print("Class B attrFormula =", B.attrFormula)


And outputs this:



Class A attr1 = 1
Class A attr2 = 2
Class A attrFormula = 0.5
Class B attr1 = 3
Class B attr2 = 3
Class B attrFormula = 1


But now, i want to change



A = Class()
A.attr1 = 1
A.attr2 = 2


by



A = Class(1,2)


because i want to disable all setter attributes and make the attributes unmodifiable. Something desired like that :



class Attr1(object):
def __init__(self, value=None):
self.value = value
def __get__(self, instance, owner):
return self.value
def __set__(self, instance, value):
raise AttributeError("You can't change attr1")
class Attr2(object):
def __init__(self, value=None):
self.value = value
def __get__(self, instance, owner):
return self.value
def __set__(self, instance, value):
raise AttributeError("You can't change attr2")
class AttrFormula(object):
def __get__(self, instance, owner):
return instance.attr1 / instance.attr2
class Class(object):
attr1 = Attr1()
attr2 = Attr2()
attrFormula = AttrFormula()

def __init__(self, attr1, attr2):
self.attr1 = attr1 # ERROR because it calls __set__
self.attr2 = attr2

A = Class(1,2)
print("Class A attr1 =", A.attr1)
print("Class A attr2 =", A.attr2)
print("Class A attrFormula =", A.attrFormula)
B = Class(5,7)
print("Class B attr1 =", B.attr1)
print("Class B attr2 =", B.attr2)
print("Class B attrFormula =", B.attrFormula)

B.attr1 = 10 # Forbidden, desired error


How to init attr1 and attr2 in Class ?










share|improve this question

























  • You want read-only attributes, so you've used a code snippet that... doesn't actually create read-only attributes? (Also you're storing data on the descriptor object instead of the instance, which is going to lead to all instances sharing the same attribute values.)

    – user2357112
    Nov 13 '18 at 23:28











  • I guess attrFormula is read-only, but it's still a really verbose and buggy way to implement what property would do much easier.

    – user2357112
    Nov 13 '18 at 23:30






  • 1





    Why not use attrs or dataclasses?

    – user2357112
    Nov 13 '18 at 23:31
















0















I want to create read only attribute.
The example below works well, and it is inspired and simplified from https://howto.lintel.in/how-to-create-read-only-attributes-and-restrict-setting-attribute-values-on-object-in-python/



class Attr1(object):
def __get__(self, instance, owner):
return self.value
def __set__(self, instance, value):
self.value = int(value)
class Attr2(object):
def __get__(self, instance, owner):
return self.value
def __set__(self, instance, value):
self.value = int(value)
class AttrFormula(object):
def __get__(self, instance, owner):
return instance.attr1 / instance.attr2
class Class(object):
attr1 = Attr1()
attr2 = Attr2()
attrFormula = AttrFormula()

A = Class()
A.attr1 = 1
A.attr2 = 2
print("Class A attr1 =", A.attr1)
print("Class A attr2 =", A.attr2)
print("Class A attrFormula =", A.attrFormula)
B = Class()
B.attr1 = 3
B.attr2 = 3
print("Class B attr1 =", B.attr1)
print("Class B attr2 =", B.attr2)
print("Class B attrFormula =", B.attrFormula)


And outputs this:



Class A attr1 = 1
Class A attr2 = 2
Class A attrFormula = 0.5
Class B attr1 = 3
Class B attr2 = 3
Class B attrFormula = 1


But now, i want to change



A = Class()
A.attr1 = 1
A.attr2 = 2


by



A = Class(1,2)


because i want to disable all setter attributes and make the attributes unmodifiable. Something desired like that :



class Attr1(object):
def __init__(self, value=None):
self.value = value
def __get__(self, instance, owner):
return self.value
def __set__(self, instance, value):
raise AttributeError("You can't change attr1")
class Attr2(object):
def __init__(self, value=None):
self.value = value
def __get__(self, instance, owner):
return self.value
def __set__(self, instance, value):
raise AttributeError("You can't change attr2")
class AttrFormula(object):
def __get__(self, instance, owner):
return instance.attr1 / instance.attr2
class Class(object):
attr1 = Attr1()
attr2 = Attr2()
attrFormula = AttrFormula()

def __init__(self, attr1, attr2):
self.attr1 = attr1 # ERROR because it calls __set__
self.attr2 = attr2

A = Class(1,2)
print("Class A attr1 =", A.attr1)
print("Class A attr2 =", A.attr2)
print("Class A attrFormula =", A.attrFormula)
B = Class(5,7)
print("Class B attr1 =", B.attr1)
print("Class B attr2 =", B.attr2)
print("Class B attrFormula =", B.attrFormula)

B.attr1 = 10 # Forbidden, desired error


How to init attr1 and attr2 in Class ?










share|improve this question

























  • You want read-only attributes, so you've used a code snippet that... doesn't actually create read-only attributes? (Also you're storing data on the descriptor object instead of the instance, which is going to lead to all instances sharing the same attribute values.)

    – user2357112
    Nov 13 '18 at 23:28











  • I guess attrFormula is read-only, but it's still a really verbose and buggy way to implement what property would do much easier.

    – user2357112
    Nov 13 '18 at 23:30






  • 1





    Why not use attrs or dataclasses?

    – user2357112
    Nov 13 '18 at 23:31














0












0








0








I want to create read only attribute.
The example below works well, and it is inspired and simplified from https://howto.lintel.in/how-to-create-read-only-attributes-and-restrict-setting-attribute-values-on-object-in-python/



class Attr1(object):
def __get__(self, instance, owner):
return self.value
def __set__(self, instance, value):
self.value = int(value)
class Attr2(object):
def __get__(self, instance, owner):
return self.value
def __set__(self, instance, value):
self.value = int(value)
class AttrFormula(object):
def __get__(self, instance, owner):
return instance.attr1 / instance.attr2
class Class(object):
attr1 = Attr1()
attr2 = Attr2()
attrFormula = AttrFormula()

A = Class()
A.attr1 = 1
A.attr2 = 2
print("Class A attr1 =", A.attr1)
print("Class A attr2 =", A.attr2)
print("Class A attrFormula =", A.attrFormula)
B = Class()
B.attr1 = 3
B.attr2 = 3
print("Class B attr1 =", B.attr1)
print("Class B attr2 =", B.attr2)
print("Class B attrFormula =", B.attrFormula)


And outputs this:



Class A attr1 = 1
Class A attr2 = 2
Class A attrFormula = 0.5
Class B attr1 = 3
Class B attr2 = 3
Class B attrFormula = 1


But now, i want to change



A = Class()
A.attr1 = 1
A.attr2 = 2


by



A = Class(1,2)


because i want to disable all setter attributes and make the attributes unmodifiable. Something desired like that :



class Attr1(object):
def __init__(self, value=None):
self.value = value
def __get__(self, instance, owner):
return self.value
def __set__(self, instance, value):
raise AttributeError("You can't change attr1")
class Attr2(object):
def __init__(self, value=None):
self.value = value
def __get__(self, instance, owner):
return self.value
def __set__(self, instance, value):
raise AttributeError("You can't change attr2")
class AttrFormula(object):
def __get__(self, instance, owner):
return instance.attr1 / instance.attr2
class Class(object):
attr1 = Attr1()
attr2 = Attr2()
attrFormula = AttrFormula()

def __init__(self, attr1, attr2):
self.attr1 = attr1 # ERROR because it calls __set__
self.attr2 = attr2

A = Class(1,2)
print("Class A attr1 =", A.attr1)
print("Class A attr2 =", A.attr2)
print("Class A attrFormula =", A.attrFormula)
B = Class(5,7)
print("Class B attr1 =", B.attr1)
print("Class B attr2 =", B.attr2)
print("Class B attrFormula =", B.attrFormula)

B.attr1 = 10 # Forbidden, desired error


How to init attr1 and attr2 in Class ?










share|improve this question
















I want to create read only attribute.
The example below works well, and it is inspired and simplified from https://howto.lintel.in/how-to-create-read-only-attributes-and-restrict-setting-attribute-values-on-object-in-python/



class Attr1(object):
def __get__(self, instance, owner):
return self.value
def __set__(self, instance, value):
self.value = int(value)
class Attr2(object):
def __get__(self, instance, owner):
return self.value
def __set__(self, instance, value):
self.value = int(value)
class AttrFormula(object):
def __get__(self, instance, owner):
return instance.attr1 / instance.attr2
class Class(object):
attr1 = Attr1()
attr2 = Attr2()
attrFormula = AttrFormula()

A = Class()
A.attr1 = 1
A.attr2 = 2
print("Class A attr1 =", A.attr1)
print("Class A attr2 =", A.attr2)
print("Class A attrFormula =", A.attrFormula)
B = Class()
B.attr1 = 3
B.attr2 = 3
print("Class B attr1 =", B.attr1)
print("Class B attr2 =", B.attr2)
print("Class B attrFormula =", B.attrFormula)


And outputs this:



Class A attr1 = 1
Class A attr2 = 2
Class A attrFormula = 0.5
Class B attr1 = 3
Class B attr2 = 3
Class B attrFormula = 1


But now, i want to change



A = Class()
A.attr1 = 1
A.attr2 = 2


by



A = Class(1,2)


because i want to disable all setter attributes and make the attributes unmodifiable. Something desired like that :



class Attr1(object):
def __init__(self, value=None):
self.value = value
def __get__(self, instance, owner):
return self.value
def __set__(self, instance, value):
raise AttributeError("You can't change attr1")
class Attr2(object):
def __init__(self, value=None):
self.value = value
def __get__(self, instance, owner):
return self.value
def __set__(self, instance, value):
raise AttributeError("You can't change attr2")
class AttrFormula(object):
def __get__(self, instance, owner):
return instance.attr1 / instance.attr2
class Class(object):
attr1 = Attr1()
attr2 = Attr2()
attrFormula = AttrFormula()

def __init__(self, attr1, attr2):
self.attr1 = attr1 # ERROR because it calls __set__
self.attr2 = attr2

A = Class(1,2)
print("Class A attr1 =", A.attr1)
print("Class A attr2 =", A.attr2)
print("Class A attrFormula =", A.attrFormula)
B = Class(5,7)
print("Class B attr1 =", B.attr1)
print("Class B attr2 =", B.attr2)
print("Class B attrFormula =", B.attrFormula)

B.attr1 = 10 # Forbidden, desired error


How to init attr1 and attr2 in Class ?







python python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 23:39









martineau

67.2k1089181




67.2k1089181










asked Nov 13 '18 at 23:24









RodrigueRodrigue

1229




1229













  • You want read-only attributes, so you've used a code snippet that... doesn't actually create read-only attributes? (Also you're storing data on the descriptor object instead of the instance, which is going to lead to all instances sharing the same attribute values.)

    – user2357112
    Nov 13 '18 at 23:28











  • I guess attrFormula is read-only, but it's still a really verbose and buggy way to implement what property would do much easier.

    – user2357112
    Nov 13 '18 at 23:30






  • 1





    Why not use attrs or dataclasses?

    – user2357112
    Nov 13 '18 at 23:31



















  • You want read-only attributes, so you've used a code snippet that... doesn't actually create read-only attributes? (Also you're storing data on the descriptor object instead of the instance, which is going to lead to all instances sharing the same attribute values.)

    – user2357112
    Nov 13 '18 at 23:28











  • I guess attrFormula is read-only, but it's still a really verbose and buggy way to implement what property would do much easier.

    – user2357112
    Nov 13 '18 at 23:30






  • 1





    Why not use attrs or dataclasses?

    – user2357112
    Nov 13 '18 at 23:31

















You want read-only attributes, so you've used a code snippet that... doesn't actually create read-only attributes? (Also you're storing data on the descriptor object instead of the instance, which is going to lead to all instances sharing the same attribute values.)

– user2357112
Nov 13 '18 at 23:28





You want read-only attributes, so you've used a code snippet that... doesn't actually create read-only attributes? (Also you're storing data on the descriptor object instead of the instance, which is going to lead to all instances sharing the same attribute values.)

– user2357112
Nov 13 '18 at 23:28













I guess attrFormula is read-only, but it's still a really verbose and buggy way to implement what property would do much easier.

– user2357112
Nov 13 '18 at 23:30





I guess attrFormula is read-only, but it's still a really verbose and buggy way to implement what property would do much easier.

– user2357112
Nov 13 '18 at 23:30




1




1





Why not use attrs or dataclasses?

– user2357112
Nov 13 '18 at 23:31





Why not use attrs or dataclasses?

– user2357112
Nov 13 '18 at 23:31












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290969%2finitialize-attributes-object-class-and-makes-it-const%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290969%2finitialize-attributes-object-class-and-makes-it-const%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues