This question already has answers here:
How to print instances of a class using print()?
(12 answers)
What is the purpose of __str__ and __repr__? [duplicate]
(8 answers)
Closed 5 years ago.
In some classes, doing str(class) will give you a string, such as 'something'. However, whenever I make a class, it only ever gives <__main__.Test object at 0x0000026FB34D70B8>.
How do I make it return a string, such as '0', or even '100'?
Short answer: by overloading the class' __str__ and/or __repr__ methods, depending on what you need.
class Frobber:
...
def __str__ ( self ):
return "<Frobber {}>".format(self.someVariable)
Related
This question already has answers here:
How does the #property decorator work in Python?
(15 answers)
How do Python properties work?
(4 answers)
Closed 4 years ago.
I can do
class Foo(object):
x = property(lambda _: 123)
f = Foo()
f.x
to get 123
However, if I try
p = property(lambda : 123)
p
I get
<property object at 0x108f2f3b8>
Now I understand that an member of a class instance is not the same thing as a regular variable but I'm not sure what exactly makes this behavior different.
Does the fact that you instantiate a class somehow do extra binding on property objects? Is it a special case or is it a behavior I can take advantage in other situations and extend? Related - are property objects useful outside of a class declaration? Or is it just for this specific case?
This question already has answers here:
How do I get the string with name of a class?
(5 answers)
Closed 4 years ago.
class A:
pass
> A().__class__.__name__
-->'A'
> A.__class__.__name__
--> 'type'
How can I get 'A' without the parens?
You're getting 'type', because the class of a class definition is... type (in other words: a class definition is a type).
You can just use the __name__ attribute. No need to look for the __class__, you already have the class:
A.__name__
'A'
Just to make it completely clear:
A().__class__ is A
True
This question already has answers here:
Can you monkey patch methods on core types in Python?
(15 answers)
Closed 8 years ago.
"this is my string".myfunction(argument)
This is very simple in javascript. With the keyword this i can access to my string directly. Is that possible with python?
You can inherit from str and define your own methods:
class myString(str):
def my_method(self, ...):
# ...
some_string = myString("StackOverflow")
print some_string.count("a") # method from string
print some_string.myMethod(...) # your defined method
This question already has answers here:
What does object's __init__() method do in python? [duplicate]
(3 answers)
Closed 9 years ago.
class subclass(superclass):
def __init__(self, arg1, arg2):
superclass.__init__(self, blah1, blah2)
What is the purpose of using superclass.__init__(self, blah1, blah2)?
I am a little confused regarding whether to use the last line or not while inheriting a superclass.
superclass.__init__(self,*args,**kwargs)
is essentially equivelent to
super(Myclass,self).__init__(*args,**kwargs)
that is it calls the supers constructor. but it skips the rest of the inheritance stack (I think super() bubbles or something... most of the time i use the first method)
**this is probably an over simplification
This question already has answers here:
List attributes of an object [duplicate]
(18 answers)
Closed 4 years ago.
For example I have next python class
class Myclass():
a = int
b = int
Imagine that I don't know the name this class, so I need to get the names of attributes? ("a" and "b")
If you want all (including private) attributes, just
dir(Myclass)
Attributes starting with _ are private/internal, though. For example, even your simple Myclass will have a __module__ and an empty __doc__ attribute. To filter these out, use
filter(lambda aname: not aname.startswith('_'), dir(Myclass))