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
Related
This question already has answers here:
obtaining named attributes of self
(2 answers)
Closed 3 years ago.
I want to know how can I access a class attribute using a string. E.g
class Test:
def __init__(self):
# defined self.name here
self.name.person = 1
If I have the program:
a = "person"
b = Test()
How can I print self.name.person using the variable a?
print(b.name.a)
Many thanks!
Python has getattr() as built-in function:
print(getattr(b.name, a))
This question already has answers here:
Can I dynamically convert an instance of one class to another?
(5 answers)
Assigning to an instance's __class__ attribute in Python
(1 answer)
Closed 4 years ago.
Is it possible to change the class of an object? In the following example:
class Fisch:
...
class Trout(Fish):
def some_special_trout_method:
...
...
class Salmon(Fish):
...
...
fisch_object = Trout()
Would it be possible to change the trout into a salmon and keep the class variable values?
If this is possible how? And if it's possible, is it a good idea?
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:
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)
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))